Process and scheduled task management
- View process
-
- The relationship between program and process
- View process information and legend
- View dynamic process ranking information (top)
- Query process PID information based on specific conditions (pgrep)
- View process tree pstree
- Control process
- Plan task management
View process
The relationship between program and process
program
Executable code and data stored in hard disk, CD-ROM and other media
Statically saved code in the file
process
Program code running in CPU and memory
Dynamically executed code
Each program can create one or more processes
Thread
Thread is the smallest unit that the operating system can perform operation scheduling
It is included in the process and is the actual operating unit in the process
A thread refers to a single sequential control flow in a process. Multiple threads can be concurrent in a process, and each thread executes different tasks in parallel.
View process information and legend
View static process statistics (ps)
ps to
ps aux
Corresponding content of each column
USER: the user of the process
PID: ID of the process
%CPU: The percentage of CPU occupied by the process
%MEM: Percentage of memory occupied
VSZ: The amount of virtual memory used by the process (KB)
RSS: The amount of physical memory occupied by the process (KB)
TTY: The name of the terminal that started the process. Processes that are not started from the terminal are displayed as?
STAT: the status of the trip
(D: uninterruptible sleep status;
R: running state;
S: in sleep state, can be awakened;
T: Stop state, it may be paused in the background or the process is in the state of tracking and debugging;
Z: Zombie process, the process has been terminated, but some programs are still in the memory)
START: The process was triggered to start time
TIME: The time the process actually uses the CPU to run
COMMAND: the start command of the process
ps -elf
ps -elf
-e: Display all process information in the system
-l: Use long format to display process information
-f: Use complete format to display process information
Corresponding content of each column
F: System tag assigned by the kernel to the process
S: The status of the process
UID: the user who started these processes
PID: Process ID of the process
PPID: the process number of the parent process (if the process was started by another process)
C: CPU utilization in the life cycle of the process
PRI: The priority of the process (the larger the number, the lower the priority)
NI: Modesty value is used to participate in determining priority
ADDR: the memory address of the process
SZ: If the process is swapped out, the approximate size of the swap space required
WCHAN: If the process is sleeping, display the name of the system function in sleep
STIME: the system time when the process started
TTY: The terminal device when the process starts
TIME: Cumulative CPU time required to run the process
CMD: The start command of the process
View dynamic process ranking information (top)
top command format:
top
Corresponding content of each line
The first line is the task queue information
20:52:05 -----system time
up 19min -----The system has been running for a long time
2 users -----The number of currently logged in users
Load average: 0.07, 0.07, 0.08 ###System load, that is, the number of tasks processed by the system in a unit time. The next three values are the average values from 1 minute, 5 minutes, and 15 minutes ago to the present
The second line is the process information
Tasks: the total number of processes
running: the number of running processes
sleeping: the number of sleeping processes
stopped: the number of stopped processes
zombie: the number of zombie processes
The third line is the CPU information
us: occupied by the user
sy: Kernel occupation
ni: priority scheduling occupation
id: idle CPU, to understand the percentage of idle CPU, mainly see the %id part
wa: I / O etc.
hi: hardware interrupt occupation
si: software interrupt occupation
st: Virtualization occupancy
The fourth line is the 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 is the information of the exchange space
total: total swap space
free: free swap space
used: used swap space
avail Mem: available physical space
The corresponding content of each column in the process information area:
PID: process id
USER: the user name of the process owner
PR: Priority
NI: Modest degree value. Negative value indicates high priority, positive value indicates low priority
VIRT: The total amount of virtual memory used by the process, in kb
RES: physical memory size used by the process, in kb
SHR: shared memory size, unit kb
S: Process status
%CPU: The percentage of CPU time occupied from the last update to the present
%MEM: The percentage of physical memory used by the process
TIME+: The total CPU time used by the process, in units of 1/100 second
COMMAND: command name/command line
Top commonly used commands:
P key: sort according to the percentage of CPU usage
M key: sort according to the size of the resident memory
N key: sort according to start time
c key: switch display command name and full command line
h key: get online help information of top program
K key: Enter the PID number of the specified process according to the prompt and press Enter to terminate the corresponding process
q key: exit top program
Number 1 key: display the number and status of the CPU
Query process PID information based on specific conditions (pgrep)
pgrep
pgrep -l "log"
-l: Display process name, only PID number is output by default
-U: Specify a specific user
-t: Specify the terminal
Fuzzy query to PID number and process name
pgrep -l -U zzw
Switch the user name in the virtual machine, and then use the user name to query related process or PID information
View process tree pstree
List process information in a tree structure
pstree -aup
-a: display complete information
-u: List the corresponding user name
-p: List the corresponding PID number
View only the process tree belonging to the specified user:
pstree -aup zzw
Switch the user zzw in the virtual machine and execute the top command
Control process
How the process is started
Manual start
Start in the foreground: the user enters a command and executes the program directly
Start in the background: add the "&" symbol at the end of the command line
Background startup can output information including background task serial number and PID number
Assume that if the content of the cp command is too large, it will take a certain amount of time to wait
At this time, if you use background startup, all processes will be executed in the background, you can execute the ps -elf command to see if it is completed
Schedule start
Use the at command to set up a one-time scheduled task
Use the crontab command to set up a periodic scheduled task
Front-end and back-end scheduling of the process
Ctrl+Z key combination
Suspend the current process, that is, transfer to the background and stop execution
jobs command
View the list of tasks in the background
jobs -l
fg command
Restore the background process to the foreground, you can specify the task number
Suspend two background processes. When the first background process is restored to the foreground process, the top command finds that there is another process in the stopped state. Only when the two background processes are restored, there is no process in the stopped state.
Terminate the operation of the process
Ctrl+C key combination
The command being executed by the terminal
kill, killall commands
kill is used to plant 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
kill -9 forcibly terminates process 2. If there is no saved content in process 2, it will be lost
The same is true for killall, killall -9 vim forcibly terminates all vim processes
pkill command
Terminate the corresponding process according to specific conditions
Common 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
Terminate all processes of the user "zzw", display the user "to kill" on the virtual machine and automatically switch back to the root user
Plan task management
at one-time task settings
at [HH:MM] [yyyy-mm-dd](不携带日期表示为当天)
The current time is 02:11:56. Use the at command to set a restart at 02:30 of the day. Press Ctrl+D to submit the task, which means that only one operation will be performed
atq: View the list of tasks to be performed
atrm 3: Delete the task of sequence number 3
crontab periodic task settings
crontab command
Repeat the command operation specified by the user according to the preset time period (minutes, hours, days, months, weeks)
It is a periodic scheduled task
Main setting file
Global configuration file, located in the file: /etc/crontab
The default setting of the system, located in the directory: /etc/cron.*/
User-defined settings, located in the file: /var/spool/cron/username
Manage crontab scheduled tasks
编辑计划任务:
crontab -e [-u 用户名]
-u缺省时默认是针对当前用户
查看计划任务:
crontab -l [-u 用户名]
删除计划任务:
crontab -r [-u 用户名]
Format of crontab task configuration
Field description
Field description
Minutes are any integer from 0 to 59.
Hours are any integers from 0 to 23.
Dates are any integers from 1 to 31.
Months are from 1 to 12. Any integer between the
day of the week is any integer from 0 to 7, 0 or 7 represents
the command or program script to be executed by the Sunday command (indicated by absolute path)
Field | Description |
---|---|
minute | The value is any integer from 0 to 59 |
hour | The value is any integer from 0 to 23 |
date | The value is any integer from 1 to 31 |
month | The value is any integer from 1 to 12 |
week | The value is any integer from 0 to 7, 0 or 7 represents Sunday |
command | The command or program script to be executed (indicated by absolute path) |
Special representation method of time value
*Indicates any time within the range
, Representing multiple discrete time points in the interval
-Represents a continuous time range
/Specify the time frequency of the interval
crontab -e [-u root]
Regularly execute the rule every minute
每1分钟执行:
*/1****或者*****
Execute the rule every hour
每小时执行:
0 * * * *或者0 */1 * * *
Regularly execute the rule once a day
每天执行:
0 0 * * *
Regularly execute the rule once a week
每周执行:
0 0 * * 0
Regularly execute the rule once a month
每月执行:
0 0 1 * *
Regularly execute the rules once a year
每年执行:
0 0 1 1 *
Cite other examples
5 * * * * ls 指定每小时的第5分钟执行一次ls命令
30 5 * * * ls 指定每天的 5:30 执行ls命令
30 7 8 * * ls 指定每月8号的7:30分执行ls命令
30 5 8 6 * ls 指定每年的6月8日5:30执行ls命令
30 6 * * 0 ls 指定每星期日的6:30执行ls命令[注:0表示星期天,1表示星期1,以此类推,也可以用英文来表示,sun表示星期天,mon表示星期一等。]
30 3 10,20 * * ls 每月10号及20号的3:30执行ls命令[注:“,”用来连接多个不连续的时段]
25 8-11 * * * ls 每天8-11点的第25分钟执行ls命令[注:“-”用来连接连续的时段]
*/15 * * * * ls 每15分钟执行一次ls命令 [即每个小时的第0 15 30 45 60分钟执行ls命令 ]
30 6 */10 * * ls 每个月中,每隔10天6:30执行一次ls命令[即每月的1、11、21、31日是的6:30执行一次ls命令。]
Output statement
echo '30 7 * * 6 /usr/bin/systemctl httpd restart' >> /var/spool/cron/root
Every week at 7:30 on 6th, the httpd service will be restarted as the root user