CentOS7 operation and maintenance-process and scheduled task management | ultra detailed | at one-time timing task | crontab regular execution program

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

  • CPUProgram 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 process
  • PID: In progressID
  • %CPU: CPUPercentage 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 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 itinerary
  • D: Uninterruptible sleep state R;: Running state S;: Sleep state and can be awakened;: TStop state, may be paused in the background or the process is in the tracking and
    debugging state Z;: Zombie process, the process has been aborted, but part of the program is still In memory
  • START:The time when the process was triggered
  • TIME: The actual CPUrunning time of the process
  • COMMAND: 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 process
  • S: The status of the process
  • UID: The user who started these processes
  • PID: Process of processID
  • PPID: The process number of the parent process (if the process was started by another process)
  • C: CPUUtilization 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, the name of the system function in sleep will be displayed
  • STIME: System time when the process started
  • TTY: The terminal device when the process starts
  • TIME: Cumulative CPUtime required to run the process
  • CMD: 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.06System 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 CPUpercentage, mainly to see %idpart
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 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: The size of the physical memory used by the process, in kb
  • SHR:Shared memory size, unit kb
  • S: Process status
  • %CPU: CPUPercentage of time occupied from the last update to the present
  • %MEM: Percentage of physical memory used by the process
  • TIME+: The CPUtotal 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

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 atcommands to set up a one-time scheduled task
  • Use crontabcommands to set up periodic scheduled tasks

Front-end and back-end scheduling of the process

Ctrl+zCombination key
to suspend the current program, that is, transfer to the background and stop execution
Example: vim test.shthen executeCtrl+z

jobsCommand
View the list of tasks in the background
Example: jobs -lView the task that was just turned off

fgCommand
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 1restoration jobs.3108

Terminate the operation of the process

ctrl+cKey combination

  • Interrupt the command being executed

kill、killallcommand

  • killUsed to terminate the process of the specified PID number
  • killallUsed to terminate all processes related to the specified name
  • -9Option to force termination

Kill the specified process test.sh

kill all the VIMprocesses

Plan task management at

at is a one-time scheduled task

at [HH:MM] [yyyy-mm-dd]

Press the Ctrl+Dsubmit 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         
#周一到周五每天1730 8 * * 1,3,5       
#周一,周三,周五8.30
0 8-18/2 * * *       
#8点到18点每两小时
0 * */3 * *          
#每三天

crontab -e [-u root] Edit a scheduled task

  1. Regularly execute the rule every minute
1分钟执行: */1 * * * *或者* * * * *5分钟执行: */5 * * * *
  1. Execute the rule every hour
每小时执行: 0 * * * *或者0 */1 * * *
每天上午7点执行:0 7 * * *
每天上午710分执行:10 7 * * *
  1. Regularly execute the rule once a day
每天执行 0 0 * * *
  1. Regularly execute the rule once a week
每周执行 0 0 * * 0
  1. Regularly execute the rule once a month:
每月执行 0 0 1 * *
  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号的730分执行ls命令
30 5 8 6 * ls        
#指定每年的68530执行ls命令
30 6 * * 0 ls        
#指定每星期日的6:30执行ls命令[注:0表示星期天,1表示星期1,以此类推,也可以用英文来表示,sun表示星期天,mon表示星期一等。]
30 3 10,20 * * ls    
#每月10号及20号的330执行ls命令[注:“,”用来连接多个不连续的时段]
25 8-11 * * * ls     
#每天8-11点的第25分钟执行ls命令[注:“-”用来连接连续的时段]
*/15 * * * * ls      
#每15分钟执行一次ls命令 [即每个小时的第0 15 30 45 60分钟执行ls命令 ]
30 6 */10 * * ls     
#每个月中,每隔106:30执行一次ls命令[即每月的1112131日是的630执行一次ls命令。 ]

Method Two:

echo ‘30 7 * * 6 /usr/bin/systemctl httpd restart’ >> /var/spool/cron/root

Guess you like

Origin blog.csdn.net/qq_42427971/article/details/113521179