Linux process and scheduled task management (9)
- 1. Process and scheduled task management
- 2. How the process is started
1. Process and scheduled task management
1.1 The relationship between procedures and processes
Program:
executable code and data stored in the hard disk, CD-ROM and other media;
statically stored code
Process:
Program code running in CPU and memory;
dynamically executed code;
parent and child processes;
each parent process can create one or more processes.
1.2 Static ps command to view process information ps aux and ps -elf
It is the most commonly used process viewing tool in the Linux system. It is mainly used to display a static snapshot containing the complete information of each process currently running. You can selectively view process information through different command options.
In actual work, pipe symbols such as grep are generally added to find the running process to see if the program is started
1.2.1 ps -aux (the process information will be displayed in a simple list form, "-" can be written or not):
The meaning of each letter is as follows.
a: View all processes in the system
u: represent the specified user
x: the user's process under all terminals (tty terminal, ps/0 remote terminal)
1. USER: user
2. PID: process number (ls -l | grep Init can see the soft link of init, 1 process is init)
3. %CPU: CPU occupancy rate of the process
4, %MEM: memory occupancy rate of the process
5, VSZ: virtual memory occupancy
6, RSS: physical memory occupancy
7, TTY: terminal (? stands for unknown, mostly local)
8. STAT: current process status, where "S": can interrupt the sleep process, "D": can not interrupt the sleep process, "s": parent process, "<": Represents high priority, "N": represents low priority, "R": represents the running process, "I": multi-linear process, "Z": zombie process, "+": foreground process.
9, START: start time
10, TIME: the total time the process occupies the cpu
11. COMMAND: the name of the process
1.2.2 ps -elf (will display the process information in the system in long format):
The meaning of each letter is as follows:
e: all process information in the current system
l: use long format to display process information
f: use complete format to display process information
1. F: the system tag assigned to the process by the kernel
2. S: PID: the process ID of the process.
3. UID: The user who started these processes.
4. PID: the process ID of the process
5. PPID: the parent process of the current process
6. C: the CPU utilization in the life cycle of the process
7. PIR: user mode (amount of human interaction) priority
8. NI: kernel mode Priority (-20~19, the lower the value, the higher the priority)
9. ADDR:-represents running
10, SZ: occupy the capacity of the swap exchange partition
11. WCHAN: the name of the current process kernel state
12. CMD: the name of the command
1.2.3 Zombie Process
A process ends, but if the parent process of the process has ended first, then the process will not become a zombie process, because when each process ends, the system will scan all the processes running in the current system to see if there are any No process is a child process of the process that has just ended. If it is, Init will take over it and become its parent process. After the child process exits, init will reclaim the relevant resources it occupied.
But when the child process ends before the parent process, and the parent process does not reclaim the child process and release the resources occupied by the child process, the child process will become a zombie process at this time.
How to kill the zombie process?
The zombie process cannot be killed with the kill command, but we can kill the father of the zombie process. After the zombie daddy hangs, the zombie process becomes an orphan process. The orphan process will not occupy system resources and will be adopted by the init program. The init program will recycle it.
1. View all R-related processes
2. View the parent process of the
zombie process 3. Kill the parent process corresponding to the
zombie process 4. The zombie process is recycled
1.3 Dynamic top command to view process ranking information
1.3.1 top command:
After entering top, you can dynamically display
P: sort according to the percentage of CPU usage
M: sort according to the size of the resident memory
N: sort according to the startup time
c: switch to display the command name and the complete command line
h: get top Online help information of the program
k: Enter the PID number of the specified process according to the prompt and press Enter to terminate the corresponding process
q: Exit the number and status of the
CPU 1: Display the number and status of the CPU
As shown in the figure below:
the five lines of content in the red box Respectively: the
first line is the task queue information:
- 15:19:46 represents the system time at the moment
- up 6:09 means the system has been running from time to time
- 1 user The number of currently logged in users
- load average 0.00 0.01 0.05 System load, that is, the task book processed by the system within a unit time, the following three values are 1 minute, 5 minutes, and the average value from 15 minutes ago to the present
The second line of progress information
- Tasks total number of processes
- running The number of running processes
- number of sleeping processes
- The number of processes stopped
- zombie the number of zombie processes
The third line of CPU information
- us user occupied
- sy kernel occupied
- ni priority scheduling occupation
- id idle CPU, to understand the percentage of idle CPU, mainly depends on the important part of %id
- wa I / o etc.
- hi hardware interrupt occupation
- si software interrupt occupation
- st virtualization occupancy
The fourth line of memory information
- total total memory space
- free free memory
- used used memory
- buff/cache The sum of the buffers of physical memory and swap memory
The fifth line exchanges space information
- total total swap space
- free free swap space
- used swap space
- avail mem available physical space
1.4 pgrep command
Query process PID information according to specific conditions
1. pgrep -l "xxx"
-l: display the process name, by default only the PID number is output
-U: specify a specific user
-t: specify a terminal
Here run vim in xshell to enter ens33 network card modification
Open another session and enter pgrep -l''vim'' and pgrep''vim'' to get the result as shown below:
You can see that adding -l can display the command name of the process.
1.5 View process tree pstree
pstree command-list process information in a tree structure
pstree -aup
-a: display complete information
-u: list the
corresponding user name -p: list the corresponding PID
2. How the process is started
2.1 Manual start
- Start in the foreground: the user enters a command and executes the program directly. This is
how we currently use the command - Background program: Add the "&" symbol at the end of the command line.
After adding &, the command can continue to be executed in the background without affecting the continued operation.
Here, when I directly copy a relatively large image file, I execute it directly, because it is relatively large and cannot be completed immediately, so it cannot be operated for a long time.
So at the moment, I put the copy image file in the background for processing, which greatly increases the efficiency of use
2.2 Front and background scheduling of the process:
2.2.1 Crtl +Z key combination
Suspend the current process, that is, transfer to the background and stop execution
2.2.2 jobs [-l]
View the list of tasks in the background
2.2.3 fg command
To restore the background process to the foreground, you can specify the task number.
Here we use the copy cp command to copy a large image file, and the experiment is suspended and resumed.
- cp /dev/cdrom myrain.iso
here copy the cdrom image file to myrain.iso - Press CTRL+Z to hang
- Enter jobs -l to
see the current process, and see that the cp task has stopped - Enter fg 1 to restore the current process labeled 1 and
you can see that the cp task continues again
2.3 Terminate the operation of the process
2.3.1 Ctrl+C key combination
- Interrupt the command being executed
2.3.2 kill, killall commands
- kill is used to terminate the process with the specified PID number
- killall is used to terminate all processes related to the specified name
- -9 option is used to force termination
Here I force kill two processes.
2.3.3 pkill command
- Terminate the process based on specific conditions
- Common command options
-U: Terminate the corresponding process according to the user name to which the process belongs
-t: Terminate the corresponding process according to the terminal where the process is located
2.4 Scheduling start
2.4.1 Using the at command
Set up a one-time scheduled task
at [HH:MM] [yyyy-mm-dd](这里“[ ]”如果不加的话默认用当前时间。)
atq:查看当前所有未执行的at任务
atrm + atq中对应的编号 :表示删除对应的at任务
Here are two at timing tasks
1. Overwrite the number of root-related processes at 2020-11-31 19:50 (using the symbol ">" to indicate overwrite) into the file ps.root, and we pass cat /tmp/ps.root can see the result 1 is the number of 156 processes
2. A directory was created at 19:51, which can also be viewed.
The following demonstrates atq and atrm instructions
2.4.2 crontab command, set periodic scheduled tasks
The crontab command
● Repeat the user-specified command operation according to the preset time period ( minute, hour, day, month, week ) ●It is a periodic scheduled task ●Main configuration file ◆ Global configuration file, located in the file: /etc/crontab ◆ The system default settings are located in the directory: /etc/cron.*/ ◆ User-defined settings are located in the file: /varlspool/cron/user name Command: crontab -e [-u user name] -----Edit scheduled task -u defaults to the current user by default
crontab -l [-u username] ------View scheduled tasks
crontab -r [-u username] ------- delete scheduled task
Format of crontab task configuration
Field | Description |
---|---|
minute | Any integer between 0~59 minutes |
hour | Any integer between 0~23 hours |
date | Any integer between 1 and 31 days |
month | Any integer between 1 and December |
week | Any integer between 0~7, 0 and 7 both represent Sunday |
command | Need to execute commands or program scripts |
2.4.3 Special representation method of time value
●* indicates any time within the range
●, indicates multiple discrete time points in the interval
●- indicates a continuous time range
●/ the time frequency of the specified interval
Example:
0 17 * * 1-5
indicates every Monday to Friday 17:00
50 10 * * 2
, 3, 5 means: every Tuesday, Wednesday, and Friday at 10:50
18 9 */4 */2 *
means: every two months at 9 o’clock every four days 18 points
0 * * / 2 * *
means: execute every two days
The >> here means appending, which is placed on the second line instead of overwriting.