CentOS7 operation and maintenance-process and scheduled task management
- The relationship between program and process
- View process information ps
- View process ranking information dynamically top
- View process information pgrep
- View process tree pstree
- How the process is started
- Manual start
- Terminate the operation of the process
- Plan task management at
- Scheduled task management crontab
The relationship between program and process
program
- It is a string of statically stored codes, executable codes and data stored in media such as hard disks and CDs
process
CPU
Program code running in and in memory- Dynamically executed code
- Separate parent process and child process
- Each program can create one or more processes
- A main process may have multiple child processes
View process information ps
View static process statistics
ps aux or -aux
#他们获取的内容一致 "-"可加可不加
ps -aux | wc -l
ps aux | wc -l
- a Display all processes on the terminal, including other user processes
- u indicates the user who lists the process
- x show all terminal processes
Linux is a multi-user login and multi-process system
Explanation of each column
USER
: The user of the processPID
: In progressID
%CPU
:CPU
Percentage occupied by the process%MEM
: Percentage of memory occupiedVSZ
: The amount of virtual memory used by the process [KB]RSS
: The amount of physical memory used by the process [KB]TTY
: The name of the terminal that started the process. If the process is not started from the terminal, it will be displayed as?
pts/1 stands for remote tool connection
STAT
: The status of the itineraryD
: Uninterruptible sleep stateR
;: Running stateS
;: Sleep state and can be awakened;:T
Stop state, may be paused in the background or the process is in the tracking and
debugging stateZ
;: Zombie process, the process has been aborted, but part of the program is still In memorySTART
:The time when the process was triggeredTIME
: The actualCPU
running time of the processCOMMAND
: The start command of the process
View static process statistics
ps -elf #必须加-否则显示不全
-e
Display all process information in the system-l
Use long format to display process information-f
Display process information in full format
Explanation of each column
F
: System tag assigned by the kernel to the processS
: The status of the processUID
: The user who started these processesPID
: Process of processID
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 processPRI
: The priority of the process (the larger the number, the lower the priority)NI
: Modesty value is used to participate in determining priorityADDR
: The memory address of the processSZ
: If the process is swapped out, the approximate size of the swap space requiredWCHAN
: If the process is sleeping, the name of the system function in sleep will be displayedSTIME
: System time when the process startedTTY
: The terminal device when the process startsTIME
: CumulativeCPU
time required to run the processCMD
: The start command of the process
Zombie process
- If the parent process of a process ends first, then this process will not become a zombie process because at the end of each process, the system will scan all the programs running in the current system to see if there are any processes that are children of the process that just ended. If it is, Init will take over him. After the child process exits, Init will reclaim the relevant resources.
- If the child process ends first, it will become a zombie process
View process ranking information dynamically top
The first line is the task queue information
① 15:56:44
: System time
② up 3:54
: System running time
③ 2 user
: Number of currently logged-in users
④load average:: 0.22, 0.08, 0.06
System load, that is, the number of tasks processed by the system in a unit time, the next three values are 1 minute, 5 minutes, and 15 minutes ago to the present average of
The second line of progress information
① Tasks
: Total number of processes
② running
: Number of running processes
③ sleeping
: Number of sleeping processes
④ stopped
: Number of aborted processes
⑤ zombie
: Number of dead processes
The third line of CPU information
① us
: user occupies
② sy
: kernel occupies
③ ni
: priority scheduling occupied
④ id
: idle CPU
, to understand idle CPU
percentage, mainly to see %id
part
⑤ wa
: the I / O wait occupancy
⑥ hi
: hardware interrupt occupied
⑦ si
: software interrupt occupied
⑧ st
: virtualization occupancy
The fourth line of memory information
① total
: Total memory space
② free
: Free memory
③ used
: Used memory
④ buff/cache
: The sum of buffers of physical memory and swap memory
The fifth act exchanges space information
① total
: Total swap space
② free
: Free swap space
③ used
: Used swap space
④ avail Mem
: Available physical space
Explanation of each column in the process information area
PID
: Process idUSER
: The user name of the process ownerPR
:priorityNI
: Modest degree value. Negative value indicates high priority, positive value indicates low priorityVIRT
: The total amount of virtual memory used by the process, in kbRES
: The size of the physical memory used by the process, in kbSHR
:Shared memory size, unit kbS
: Process status%CPU
:CPU
Percentage of time occupied from the last update to the present%MEM
: Percentage of physical memory used by the processTIME+
: TheCPU
total time used by the process , in units of 1/100 secondCOMMAND
: 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
View process information pgrep
Query process PID information according to specific conditions
pgrep -l 进程关键字 #指定查看进程名
-l
Show process name-U
Specify a specific user-t
Designated terminal
View process tree pstree
pstree -aup [用户名]
-a
: Show complete information-u
: List the corresponding user names-p
: List the corresponding PID number
Here is a tool atop similar to top with more functions
#如何安装
yum install -y atop
How the process is started
Manual start
- Start in the foreground: the user enters a command, the byte executes the program
- Start in the background: add
"&"
symbols at the end of the command line [improve work efficiency]
Example: When downloading software from the external network, download it in the background.
At this time, you can only wait and cannot perform other operations
. You can execute other commands at any time after switching to the background.
Schedule start
- Use
at
commands to set up a one-time scheduled task - Use
crontab
commands to set up periodic scheduled tasks
Front-end and back-end scheduling of the process
Ctrl+z
Combination key
to suspend the current program, that is, transfer to the background and stop execution
Example: vim test.sh
then executeCtrl+z
jobs
Command
View the list of tasks in the background
Example: jobs -l
View the task that was just turned off
fg
Command
To restore the background process to the foreground, you can specify the task number.
Example: You can see the tasks that have been turned off during the fg 1
restoration jobs
.3108
Terminate the operation of the process
ctrl+c
Key combination
- Interrupt the command being executed
kill、killall
command
kill
Used to terminate the process of the specified PID numberkillall
Used to terminate all processes related to the specified name-9
Option to force termination
Kill the specified process test.sh
kill all the VIM
processes
Plan task management at
at is a one-time scheduled task
at [HH:MM] [yyyy-mm-dd]
Press the Ctrl+D
submit task button
, for example:
Scheduled task management crontab
Repeat the command operation specified by the user according to the preset time period (minute, hour, day, month, week)
Periodic scheduled task
Main setting file
Global configuration file, located in file: /etc/crontab
system default settings, located in directory: /etc/cron.*/
user-defined settings, located in file:/var/spool/cron/用户名
Manage crontab scheduled tasks
Edit scheduled tasks
crontab -e 【-u 用户名】
#-u缺省时默认当前用户
View scheduled tasks
crontab -l 【-u 用户名】
Delete scheduled task
crontab -r 【-u 用户名】
Format of crontab task configuration
Representative meaning | minute | hour | date | month | week | command |
---|---|---|---|---|---|---|
Number range | 0~59 | 0~23 | 1~31 | 1~12 | 0~7 | Commands to be executed |
When the number of the week is 0 or 7, it means "Sunday"
Special representation method of time value
*
Represents any time within the range,
,
represents multiple discrete time points in the interval,
-
represents a continuous time range,
/
represents the time frequency of the interval
Application example
0 17 * * 1-5
#周一到周五每天17点
30 8 * * 1,3,5
#周一,周三,周五8.30
0 8-18/2 * * *
#8点到18点每两小时
0 * */3 * *
#每三天
crontab -e [-u root] Edit a scheduled task
- Regularly execute the rule every minute
每1分钟执行: */1 * * * *或者* * * * *
每5分钟执行: */5 * * * *
- Execute the rule every hour
每小时执行: 0 * * * *或者0 */1 * * *
每天上午7点执行:0 7 * * *
每天上午7点10分执行:10 7 * * *
- 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 *
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命令。 ]
Method Two:
echo ‘30 7 * * 6 /usr/bin/systemctl httpd restart’ >> /var/spool/cron/root