Super detailed Linux process and scheduled task management

1. The relationship between procedures and processes

(1) Procedure

1. Executable code and data stored in the hard disk, CD, etc.
2. Code stored statically in the file

(2) Process

1. Program code running in CPU and memory
2. Dynamically executed code
3. Parent and child processes
  Each program can create one or more processes

Two, view process information

(1) View the static process statistics ps aux command

ps  aus

ps command-----------View static process statistics
method one:
ps to

a: Display all processes on the terminal, including processes of other users.
u: indicates the user who lists the processes.
x: displays the processes of all terminals.

Insert picture description here

Explanation of each column
USER: process user
PID: process ID
%CPU: CPU percentage occupied by the process
%MEM: percentage of memory occupied
VSZ: virtual memory used by the process (KB)
RSS: physical memory occupied by the process (KB)
TTY: The name of the terminal that started the process. The process that is not started from the terminal is displayed as?
STAT: the status of the trip (D: non-interruptible sleep state; R: running state; S: sleeping state and can be awakened; T: stopped state, possibly in the background Paused or the process is in a tracking and debugging state; z: zombie process, the process has been terminated, but some programs are still in the memory)
START: the process is triggered start time
TIME: the actual CPU running time of the
process COMMAND: process start command

(Two), view the process information ps -elf command

View static process statistics

ps -elf

Method Two:
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

Insert picture description here

Explanation of each column:
F: the system mark assigned to the process by the kernel
S: the status of the process
UID: the user who started these processes
PID: the process ID of the process
PPID: the process number of the parent process (if the process is 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: the modest degree value is used to determine the priority
ADDR: the memory address of the
process SZ: what if the process It 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: The system time when the process is started.
TTY: The terminal device when the process is started.
TIME: The cumulative amount required to run the process. CPU time
CMD: start command of the process

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 , See if any process is a child process of the process that just ended, if so, justTake over by InitIt becomes its parent process. After the child process exits, init will reclaim the related 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.

(Three), view the process information top command

ViewdynamicProcess ranking information

top

top command-----------View process dynamic information
Insert picture description here

The first line is the task queue information
11:06:48 System time
up 1:22 The system has been running for
1 user The number of currently logged-in users
load average: 0.06 0.60 0.48 System load, that is, the number of tasks processed by the system within a unit time, the next three values ​​are 1 minute and 5 Minutes, 15 minutes ago to the current average

The second line of progress information
Tasks Total number of processes
running Number of running processes
sleeping Sleeping number of processes
stopped Number of aborted processes
zombie Number of zombie processes

The third line of CPU information
us user occupies
sy kernel occupies
ni priority scheduling occupies
id idle CPU, to know the percentage of idle CPU, mainly see %id part
wa I/0 waiting to occupy
hi hardware interrupt occupy
si software interrupt occupy
st virtualization occupation

The fourth line of memory information
total Total memory space
free Free memory
used Used memory
buff/cache The sum of physical memory and swap memory buffer

The fifth line of information exchange space
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 user name of the process owner
PR priority
NI modest value. Negative value means high priority, positive value means the
total amount of virtual memory used by the low-priority VIRT process, in kb
RES process physical memory size, unit kb
SHR shared memory size, unit kb
S process status
%CPU last updated to The current percentage of CPU time occupied
%MEM The percentage of physical memory
used by the process TIME+ The total CPU time used by the process, in 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 the startup time
c key: switch to display the command name and complete command line
h key: get online help information for the top program
k key: input the PID number of the specified process according to the prompt and press Enter to terminate the corresponding process
q key: exit the top program
Number 1 key: display the number and status of the CPU

tty terminal:
Centos7 system, tty1 means graphical interface, tty2-tty6 means text interface, you can useCtrl + Alt + F1-F6Switch.
pts indicates that it is connected with a remote tool, such as xshell, the number after it represents the time sequence of login, the smaller it proves the earlier the login

(4) pgrep command to view process information

Query process PID information according to specific conditions

pgrep -l  "log"     #-l:显示进程名,缺省时只输出PID号
pgrep  -l  -U  gcc  -t  tty2    #--U:指定特定用户   -t:指定终端

Insert picture description here

(5) View the process tree pstree command

List process information in a tree structure

pstree -aup    #-a:显示完整信息   -u:列出对应用户号  -p:列出对应PID号
pstree  -ap  gcc     #只查看属于指定用户的进程树结构

Insert picture description here
Insert picture description here
Insert picture description here

3. How to start the process

(1) Manual start

1, reception start: user input commands directly executes the program
2, backgrounding: the end of the command line was added "&"symbol

cp /dev/cdrom mycd.ios &    #输出信息中包括后台任务序号、PID号

(Two), scheduling start (at command, crontab command)

1. Use at command, Set up a one-time scheduled task
2, usecrontab command, Set up periodic scheduled tasks

Fourth, the front and background scheduling of the process

1. Ctrl+Z key combination

Suspend the current process, that is, transfer to the background and stop execution

2. The jobs command

jobs -l
view the list of tasks in the background

3. The fg command

Restore the background process to the foreground, you can specify the task number

jobs 

fg 1

Five, terminate the operation of the process

1. Ctrl+C key combination

Interrupt the command being executed

2. Kill, killall commands

kill Used to terminate the process with the specified PID number
killall Used to terminate all processes related to the specified name
-9 options For forced termination

atq View the list of unexecuted tasks
atrm+task number Delete unexecuted tasks

Six, planned task management

(1) One-time scheduled task management at command

1. One-time scheduled task

at [HH:MM] [yyyy-mm-dd]    #按Ctrl+D键提交任务

2. At case example
, the current system is automatically closed at 21:30 of the day

at 21:30   #不携带日期表示为当天
at> shutdown -h now
at> <EOT>  #按Ctrl+D 键提交任务

atq   #查看未执行的任务列表

atrm 1   #删除第一条任务

(2) Plan task management crontab command

1, crontab command

According to the preset time period (Minute, hour, day, month, week) Repeated execution of user-specified command operations
is a periodic scheduled task.
Main settings file
Global configuration file, located in file: /etc/crontab.
System default settings, located in the directory: letc/cron.*/
User-defined settings, located in file: / var/spool/cron/username

2. Manage crontab scheduled tasks

(1)、Edit scheduled tasks

crontab -e -u 用户名   #-u缺省时默认是针对当前用户

(2)、View scheduled tasks

crontab -l -u  用户名

(3)、Delete scheduled task

crontab  -r -u 用户名

Insert picture description here
Insert picture description here

3. The format of crontab task configuration
Insert picture description here

Field Description
minute The value is an 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 Command or program script to execute

(1)、Special representation method of time value
*Represents any time within the range
,Represents multiple discontinuous points in the interval
-Represents a continuous time range
/Specify the time frequency of the interval
(2),Application example
0 17 * * 1-5Monday to Friday at 17:00
30 8 * * 1,3,5every Monday, Wednesday, and Friday from 8:30
0 8-18/2 * * *to 18:00 every 2 hours
0 * */3 * *every 3 days

(3)、crontab task configuration
Significance: Minutes, Hours, Dates, Months and Weeks Commands
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".

crontab -e [ -u root ]
1) Regular execution of the rule
every minute: execution every 1 minute: execution */1 * * * * 或者 * * * * *
every 5 minutes:*/5 * * * *

2) Execute rules
every hour : Hourly execution: Execute 0 * * * * 或者 0 */1 * * *
every day at 7 am: Execute 0 7 * * *
every day at 7:10 am:10 7 * * *

3), the rule
is executed regularly every day : every day0 0 * * *

4), the rule will be executed once a
week : weekly0 0 * * 0

5) Execute the rule regularly
every month : execute every month0 0 1 * *

6), the rules
are executed regularly every year : every year0 0 1 1 *

7) Other examples
5 * * * * ls Specify the ls command to
30 5 * * *lsbe executed at the 5th minute of every hour. Specify the ls command to be executed at 5:30 every day. Specify the ls command to execute
30 78 * * lsat 7:30 on the 8th of each month. Designate to execute ls at 5:30
30 5 8 6 * lson June 8th every year Command
30 6 * * 0 lsSpecifies to execute the ls command at 6:30 every Sunday [Note: 0 means Sunday, 1 means week 1, and so on. It can also be expressed in English, sun means Sunday, mon means Monday, etc. ]
30 3 10,20 * * lsExecute the ls command at 3:30 on the 10th and 20th of each month [Note: ", "used to connect multiple discontinuous time periods]
25 8-11 * * * lsRun the ls command at the 25th minute from 8-11 every day [Note: "-" To connect to a continuous period]
*/15 * * * * lsexecute the ls command every 15 minutes [ie execute the ls command at the 0th 15 30 45 60 minutes of each hour]
30 6 */10 * * lsevery month, execute the ls command at 6:30 every 10 days [ie every month On 1, 11, 21, and 31, the ls command is executed once at 6:30. ]

Method Two:

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

Guess you like

Origin blog.csdn.net/Gengchenchen/article/details/110423723