preface
Only a bald head can be strong
To recap:
- After reading this article Linux basic operation will be
- Linux user and permission management read you will use
This article mainly explains the process management under Linux.
So let’s get started. If there are any mistakes in this article, please feel free to correct them in the comments section
Declaration: based on Centos6.9
Processes in Linux
Each user can run multiple programs simultaneously. To distinguish each running program, Linux gives each process a unique identification, called a process ID.
Linux marks each process as a runner, and users can control their own processes: they can assign different priorities to their own processes, and they can terminate their own processes at any time.
- The process inherits uids and Gids from the user executing it to determine access to and access to the file system.
Linux makes it impossible to process multiple task (job) requests simultaneously on a SINGLE CPU, and instead uses “time-sharing” technology to process these task requests.
Use pids to distinguish between different processes
- The first process after the system starts up is init, and its PID is 1. Init is the only process that is run directly by the system kernel.
- Every process except init has a parent (identified by PPID)
- There are four more for each processAn identification number associated with a user or group
- Real User ID (RUID)
- Real Group ID (RGID)
- Effect User ID (EUID)
- Effect Group ID (EGID)
- RUID and RGID are used to identify the users and groups that are running this process.
- The EUID and EGID are used to determine the permissions of a process on the files it accesses.
Process type:
- The interaction process
- A process started by a Shell.
- Interactive processes can run in the foreground or in the background.
- Batch process
- A process that is not associated with a particular terminal and is submitted to wait queue in order of execution.
- Daemons
- A process that runs in the background when Linux is initialized at startup.
Process startup mode:
- Manual mode: Use the user interface provided by the operating system
- The front desk
- Background (&)
- Scheduling mode: The system executes at a specified time
- at
- batch
- cron
Foreground process:
- A program that controls standard input/output and temporarily suspends the shell while the program is running and then returns to the shell when the program is finished. During this process, the user can no longer execute other programs
Background process:
- Users can execute other programs without waiting for them to finish running. The way to run a background process is to put an ampersand at the end of the command line.
The difference between process and job:
- Process: Concept of operating system, managed by operating system
- Homework:The concept of shell programs, managed by the shell program
- An operating system can launch multiple shell programs, and the shell itself is a process
- A job contains at least one process and can contain multiple processes
- Homework is divided into foreground and background operation
With the knowledge above, we can draw a flowchart like this:
1.1 Common Commands for Managing Processes
Run the ps command to check the process status.
ps -ef
ps aux
Ps Command parameters:
The ps command output is as follows:
There are several other common commands:
- Continue running the process after logging out:
Nohup command [options] [parameters] [output file] &
- Use SpringBoot jar package deployment to Linux under the students must be familiar with the above command ~
- Kill the process:
kill pid -9
free
: Check the memory usagetop
: Displays information about system processes that are refreshed in real time- Priority for process scheduling
nice
Command: - Adjust the priority after the process is running:
renice
Command.
Job control refers to controlling the behavior of currently running processes, also known as process control.
- Temporarily stop the use of a running program
Ctrl+z
- List job numbers and names:
jobs
- Resume running in the background:
Bg [% Job Number]
- Back up in the foreground:
Fg [% Job number]
- Sending signals:
Kill -[signal] PID
1.2 Process exercises
Log in to the system as user root and run the ps command with the options -a and -af. Observe the comparison results and take screenshots; Run the ps command with the option -ef and observe the result. Run ps with au to see the results and take a screenshot.
Switch to the jkXX account, list the /usr/bin/passwd command in long format, and observe the permission properties of the command. Run the passwd command in background mode. Run the ps command, option for ao, parameters for the user, pid, ruid, euid, tty, CMD, observations and screenshots
Peter account, switch to the background using ls -l command/usr/bin | more &, observation results; Execute the above command twice and observe the result. Run the ps command with the option au to observe which processes are running. Run the jobs command to view jobs that are running. Transfer the background command to the foreground through fg command to end it and observe the remaining operations; Until the homework is done.
Switch to Jason account, use nice command to start vi editing software in the background, increase the priority by 10, use ps command, the option is -l to observe the priority of vi process; Start vi editing software in the background with nice command, the priority is reduced by 10, observe the result, why is not successful; Switch to the root account and run the nice command to start the VI editing software in the background. The priority is reduced by 10.
What does TTY stand for in the ps command?
A: TTY stands for character terminal.
Ps command output, which process is in the running state? Which process is dormant?
A: The ps au process is running and is represented by R. The rest is dormant.
What is the euID of the passwd command, and whose permissions should it obtain during execution?
A: EuID is 0, which should be executed with the permissions of the file owner root.
If you run the chmod command to remove the suid attribute of the passwd command and run the passwd command as a common account, can you change the password? Why is that?
A: No, because the passwd command is used to modify the shadow file. The shadow file belongs to user root. Running the passwd command for a common account does not have the permission of user root to modify the shadow file.
What permissions are required for the nice command to reduce the priority value?
A: The root user permission is required
Daemons
Programs that always run in the background and respond to legitimate requests are called daemons. Daemons are not started and run by users and are not associated with terminals.
- A real running system will typically have multiple daemons running, and the daemons running vary from system to system.
- Unless programs abort abnormally or are terminated artificially, they continue to run until the system shuts down.
- UNIX/Linux daemons are called “services” on Windows systems.
Classification of daemons:
- System daemon
- Scheduled daemon: Such as ATD and Crond
- System log Daemon: rsyslogd
- Print spooling daemons such as Cupsd and LPD
- Setting network parameters daemon: for example, network
- Network daemon:
- Various network protocol listening daemons
- For example, SSHD, HTTPD, postfix, VSFTPD
- Network Supper Server
- For example, xinetd or Inetd
Introduction of xinetd for super servers:
- For every network service a system provides, it must run a daemon that listens for a port connection to occur, which usually means a waste of system resources.
- “Super server” was introduced to avoid wasting system resources. The superserver starts up and listens on all ports of the services it manages
- When a customer requests service
- The superserver determines which service it is requesting and then starts the daemon corresponding to that service
- The process of a service generated by the superserver processes the client’s request and terminates the service process when the processing is complete
- The superserver itself continues to listen for other service requests
How to start the daemon:
- The stand-alone boot
- Independently run daemons are managed by the init script, which is stored in the init script
/etc/rc.d/init.d/
directory - All system services run independently. Such as Crond and SYSlogd
- Some common network daemons run independently. Such as HTTPD, etc
- Independently run daemons are managed by the init script, which is stored in the init script
- The transient start
- The daemon run by the network superserver (xinetd), and the configuration file for the daemon managed by xinetd exists
/etc/xinetd.d/
directory - The default xinetd master profile is
/etc/xinetd.conf
- Some uncommon network daemons are started by xinetd, such as Telnet and TFTP
- Xinetd itself is a standalone daemon
- The daemon run by the network superserver (xinetd), and the configuration file for the daemon managed by xinetd exists
2.1 Common Commands for Managing Daemons
Function of the chkconfig command
- Adds the specified new service
- Clears the specified service
- Displays the services managed by chkconfig
- Change the runlevel of the service
- Check the service startup status
Chkconfig –list displays the corresponding run level:
- 0: turn it off
- 1: single user
- 2: multiple users without a network
- 3: command line mode
- 4: unused
- 5: GUI (Graphical desktop mode)
- 6: restart
Manage daemons with NTSYSV
Manage the daemon using service
service --status-all
service server-name status
service server-name start|stop|restart
2.2 Daemons Exercises
Install the daemon tool NTsysv and run it; On the NTSYSV interface, disable the automatic startup of Crond and run the chkconfig command to check whether the automatic running of Crond is disabled. The chkconfig command is used to set the crond startup and ntsysV is used to check the result. Observe the results and take screenshots
Edit the xinetd main configuration file, modify the link instances = 2, and restart xinetd. Use putty to connect to Linux in Telnet mode. Run the pstree command to check the number of Xinetd Telnet processes. Continue to open Telnet connections until the connection limit is exceeded
Edit the system service file services, change the Telnet service port to 27, and restart xinetd. Use putty to connect to Linux in Telnet mode to display the current network connection. Restore the default Telnet service port and check it. Observe the results and take screenshots.
Can the Telnet service port be changed? If so, what should I pay attention to when connecting to the Telnet service?
A: The Telnet service port can be changed. When connecting to the Telnet service, change the port number to the one that provides the Telnet service.
Do I need to restart the Xinetd service to modify the Telnet configuration file? Why is that?
A: The Xinetd service needs to be restarted because xinetd, as the super server, manages the startup of the Telnet service and also views the Telnet service configuration file. When the Telnet service configuration file is modified, the xinetd service needs to know the change of the configuration file. Restarting the Telnet service reads the configuration file again for it to take effect.
Why can Telnet view the script execution result?
A: Telnet is a network tool for remote character interface. It implements the standard input and output functions of remote character interface. The result of script execution is output to the standard output device, which is the screen of the character interface. Telnet passes the standard output over the network to the Screen of the Telnet client for display, so that it can see the result of script execution.
Is the result of running the ntsysv and chkconfig daemons exactly the same? What’s the difference?
Answer: The ntntsysv command to shut down the daemons only changes the daemons switch for the current user runlevel, specifically for runlevel 3 command line mode. By default, the chkconfig command changes all switches at levels 3, 4, and 5. Chkconfig can also specify daemon switches at a certain run level.
Schedule automated tasks
Daemons that schedule tasks:
- atd
- crond
Several commands for scheduling tasks:
- At schedules a job to be executed once at a time
- Batch schedules jobs to be executed once when the system load is not heavy
- Cron schedules jobs that run periodically
3.1 ATD daemon
The ATD daemon is responsible for monitoring the execution of one-off tasks. The execution parameter of the ATD daemon is /etc/sysconfig/atd
Control the usage of ordinary users
- if
/etc/at.allow
Yes, only users listed in it are allowed to use it - if
/etc/at.allow
Not present, check/etc/at.deny
Not all users listed in this list are allowed to use - If neither file exists, only the root user is allowed to use it
- empty
/etc/at.deny
File that allows all users to use it (default)
How to use:
- Install command
yum install at
- The start of the atd
service atd start
- View atD services
chkconfig --list | grep atd
orps -aef | grep atd
at
Command format and parametersAt [-q queue] [-f file name] Time
3.2 the cron
- The Crond daemon is responsible for monitoring the execution of periodic tasks
- The crond daemon’s execution parameter configuration file
/etc/sysconfig/crond
Control the usage of ordinary users
- if
/etc/cron.allow
Yes, only users listed in it are allowed to use it - if
/etc/cron.allow
Not present, check/etc/cron.deny
Not all users listed in this list are allowed to use - If neither file exists, only the root user is allowed to use it
- empty
/etc/cron.deny
File that allows all users to use it (default)
Once crond starts, it wakes up every minute to detect changes in the following files and load them into memory
/etc/crontab
: is a crontab file (man 5 crontab)/etc/cron.d/*
: is a crontab file (man 5 crontab)/var/spool/cron/*
: is a crontab file (man 5 crontab)/etc/anacrontab
: is a file in Anacrontab format (man 5 Anacrontab)
3.3 Schedule automatic task exercises
Install the AT Scheduled task service and start it; At tea time, check the network card information; After two minutes, turn on the firewall; The server was restarted at 2am on June 1, 2019; Tomorrow at 9 p.m., shut down eth1; Check waiting scheduled tasks with commands; Check the specific content of the waiting task with AT-C; Delete one of the tasks and observe the results. Observe the results and take screenshots
Edit a file myatXX (XX is the last two digits of the student id) and execute the following commands in order: return to the user home directory; View user account name; View all processes running in the current system and save the records to the ps.log file. Add a scheduled task that requires execution of the commands in the file at 5pm; Observe the results and take screenshots
Log in with regular account jsjXX, add a scheduled task, close eth0 at noon tomorrow; Modify the configuration file of AT command, forbid jsjXX to execute at command, and test it
Modify the crontab file and add a periodic task. The requirements are as follows: Save the information about the ports monitored by the server to the /root/net. TXT file every six hours. Every Friday or 10, 20, 30 from 9:00 am to 15:00 PM, at the 10th minute of every hour, clear all files in/TMP
Add a script that asks: Perform this task every month, find the file in /home that has not been modified in 30 days, and delete it; The script name is clean-home
Log in to jSJ08 account, and run the crontab -e command to compile a periodic task, which needs to be executed every 2 minutes in sequence: display the current system time; Display account name; Displays the process information of the current account. The result of the above command is saved to the user’s home directory file named myplanXX (XX is the last two digits of the student number). Check the written tasks with crontab -l
Does myatXX file need to change permissions before it can be executed?
A: No. The ATD service simply reads from the myatXX file. The actual command execution is stored in another file, which we can see with the at-c command.
Which directory is the script in? Does the script need to add executable properties?
A: The scripts are stored in different directories at different intervals. In this case, the scripts are executed every month. Therefore, the scripts are stored in the /etc/cron.monthly directory. The script needs to add executable properties, because this is a truly executable script.
Can a common user modify the configuration file of the AT service?
A: You need root permission to modify the AT configuration file. Common users cannot modify the AT configuration file.
Four,
This article mainly summarizes the Linux operating process and automatic task knowledge ~~~ these two knowledge points in Linux is also very important, is the foundation of learning Linux ~
Continue to improve the last mind map:
If the article has the wrong place welcome to correct, everybody exchanges with each other. Students who are used to reading technical articles on wechat and want to get more Java resources can follow the wechat public account :Java3y.
Article table of Contents navigation:
- Zhongfucheng.bitcron.com/post/shou-j…