Linux process and scheduled task management (detailed legend)

1. The relationship between process 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
  • Parent and child processes
  • Each program can create one or more processes

Two, view process information-ps

■ps command

  • View static process statistics

2.1 Method 1: ps aux

方法一:ps aux
a:显示终端上的所有进程,包括其它用户的进程。
u:表示列出进程的用户。
x:显示所有终端的进程。

Insert picture description here

parameter Explanation
USER The user of the process.
PID The ID of the process.
%CPU The percentage of CPU occupied by the process.
%MEM The percentage of memory occupied.
VSZ The amount of virtual memory (KB) used by the process.
RSS The amount of physical memory (KB) occupied by the process.
TTY The name of the terminal that started the process. Processes that are not started from the terminal are displayed as?
STAT The state of the trip
(D: non-interruptible dormant state; R: running state; S: in dormant state and can be awakened; T: stopped state, which may be paused in the background or the process is in a tracking and debugging state; Z: zombie process , The process has been aborted, but some programs are still in memory)
START The time the process was triggered to start.
TIME The process actually uses the CPU running time.
COMMAND The start command of the process.

2.2 Method 2: ps -elf

方法二:ps -elf
-e:显示系统内的所有进程信息。
-l:使用长格式显示进程信息。
-f:使用完整的格式显示进程信息。

Insert picture description here

parameter Explanation
F The system tag 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 was started by another process).
C The 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 participate in determining the 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.
ESTIMATES The system time when the process was 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.

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 there is any process that is a child process of the process that just ended. If so, Init will take over it and become 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 at this time.

Three, view process information-top

3.1 top command

  • View dynamic process ranking information
[root@localhost~]# top

3.2 top commonly used commands

button effect
P key Sort according to the percentage of CPU usage
M key Sort according to the size of the resident memory
N key Sort by start time
c key Toggle display of command name and full command line
h key Online help information for top program can be obtained
k key Enter the PID number of the specified process as prompted and press Enter to terminate the corresponding process
q key Exit top program
Number 1 key Display the number and status of CPU
## The information corresponding to each line! [Insert the picture description here](https://img-blog.csdnimg.cn/20210201212359218.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nImV9Z0size color_FFFFFF,t_70)
  • 第一行是任务队列信息
    21:22:36 : 系统时间
    up 42 min : 系统已运行时长
    4 user :当前登录用户数
    load average: 0.00, 0.01, 0.05 :系统负载,即单位时间内系统处理的任务数,后面三个数值分别为1分钟、5分钟、15分钟前到现在的平均值
  • 第二行为进程信息
    Tasks :总进程数
    running :正在运行的进程数
    sleeping :休眠的进程数
    stopped :中止的进程数
    zombie :僵死的进程数
  • 第三行为CPU的信息
    us :用户占用
    sy :内核占用
    ni :优先级调度占用
    id :空闲CPU,要了解空闲的 CPU 百分比,主要看%id 部分
    wa:I/O 等待占用
    hi :硬件中断占用
    si :软件中断占用
    st :虚拟化占用
  • 第四行为内存的信息
    total:总内存空间
    free :空闲内存
    used:已用内存
    buff/cache:物理内存和交换内存的缓冲区总和
  • 第五行为交换空间的信息
    total:总交换空间
    free :空闲交换空间
    used:已用交换空间
    avail Mem:可用物理空间

进程信息区各列解释

Insert picture description here

进程信息区参数 解释
PID 进程id
USER 进程所有者的用户名
PR 优先级
NI 谦让度值。负值表示高优先级,正值表示低优先级
VIRT 进程使用的虚拟内存总量,单位kb
RES 进程使用的物理内存大小,单位kb
SHR 共享内存大小,单位kb
S 进程状态
%CPU 上次更新到现在的CPU时间占用百分比
%MEM 进程使用的物理内存百分比
TIME+ 进程使用的CPU时间总计,单位1/100秒
COMMAND 命令名/命令行

四、查看进程信息——pgrep

■pgrep命令

  • gprep根据特定条件查询进程PID信息
pgrep -l "进程关键字"    指定查看进程名
-l	   显示进程名称
-u	   指定进程的有效用户ID

这边我们在1 centos7-1中打开top进程
Insert picture description here
pgrep -l
这边在2 centos7-1中用pgrep查看进程
Insert picture description here
pgrep -l -U 指定用户
这里我们在1 centos7-1切换到用户张三打开top进程
Insert picture description here
这边在2 centos7-1中用pgrep -l -U查看进程
Insert picture description here

五、查看进程树——pstree

pstree以树形结构列出进程信息
pstree -aup
-a   显示完整信息
-u   列出对应用户名
-p   列出对应PID号

Insert picture description here

六、进程的启动方式

6.1 手动启动

  • 前台启动:用户输入命令,直接执行程序
  • 后台启动:在命令行尾加入“&”符号

Insert picture description here
这边我在命令后面加了&可以看到可以把操作放在后台去执行
Insert picture description here

6.2 调度启动

  • 使用at命令,设置一次性计划任务
  • 使用crotab命令,设置周期性计划任务

6.3 进程的前后台调度

按键或命令 作用
Ctrl+Z组合键 将当前进程挂起,即调入后台并停止执行
jobs命令 查看处于后台的任务列表
jobs -l(加上-l显示进程号,某些命令需要进程号)
fg命令 将后台进程恢复到前台运行,可指定任务序号

演示:Ctrl+Z组合键
这边我们创建一个abc文件进入后Ctrl+Z可以看到程序被挂起了
Insert picture description here
演示:jobs -l查看处于后台的任务列表
Insert picture description here
演示:fg 1 将后台进程恢复到前台运行
Insert picture description here
可以看到原来挂起的程序恢复了
Insert picture description here

七、终止进程的运行

7.1 ctrl+c key combination

  • Interrupt the command being executed

7.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

Demo: kill -9 加上PID号
Insert picture description here
Insert picture description here
Demo:killall -9 加上指定名称
Insert picture description here

7.3 pkill command

  • Terminate the corresponding process according to specific conditions
常用命令选项
-U:根据进程所属的用户名终止相应进程
-t:根据进程所在的终端终止相应程序

Demo:

This is the process that we killed the specified user zhangsan. Insert picture description here
Let’s take a look at another window to see the status of user Zhang San, we can see that the process has been killed.Insert picture description here

8. Plan task management

8.1 One-time planning task-at

■at command

  • One-time scheduled task
at 【HH:MM】【yyyy-mm-dd】   如果后面不跟年月日,那么默认使用当前日
HH:小时  MM:分钟  yyyy:年  mm:月  dd:日
[root@localhost~]#at 10:35 2017-05-07      
at> init 6
ctrl d                          提交一次性计划任务
at> <EOT>
job 1 at Sun May 7 10:35:00 2017

[root@localhost~]# atq          查看未执行的任务列表
[root@localhost~]# atrm 1       删除序号为1的任务

Demo: Create a one-time task Task: 12:12 on December 12, 2021 Restart
Insert picture description here
atq: view the list of
Insert picture description here
unexecuted tasks atrm 1: delete the task with sequence number 1
Insert picture description here

8.2 Scheduled task management-crontab

■ crontab command

  • Concept: According to the preset time period (minute, hour, day, month, week) to repeat the command operation specified by the user
  • It is a periodic scheduled task
  • Main configuration file
    Global configuration file, located in the file: /etc/crontab
    system default settings, located in the directory: /etc/cron.*/user-defined
    settings, located in the file: /var/spool/cron/username

8.2.1 Manage crontab scheduled tasks

编辑计划任务:crontab -e [-u 用户名]     # -u默认的是当前用户
查看计划任务:crontab -l [-u 用户名]
删除计划任务:crontab -r [-u 用户名]
方法二:
echo ‘30 7 * * 6 /usr/bin/systemctl httpd restart’ >> /var/spool/cron/root

8.2.2 Format of crontab task configuration

Field minute hour date month week task
Ranges 0~59 0~23 1~31 1~12 0~7 Command or script executed

Note: When the number of the week is 0 or 7, it means "Sunday"

8.2.3 Special representation method of time value

* 表示该范围内的任意时间
, 表示间隔的多个不连续时间点
- 表示一个连续的时间范围
/ 表示间隔的时间频率

8.2.4 Application examples

0 17 * * 1-5         周一到周五每天17点
30 8 * * 1,3,5       周一,周三,周五8.30
0 8-18/2 * * *       8点到18点每两小时
0 * */3 * *          每三天
1.每分钟定时执行一次规则:
每1分钟执行: */1 * * * *或者* * * * *
每5分钟执行: */5 * * * *

2.每小时定时执行一次规则:
每小时执行: 0 * * * *或者0 */1 * * *
每天上午7点执行:0 7 * * *
每天上午7点10分执行:10 7 * * *

3.每天定时执行一次规则:
每天执行 0 0 * * *

4.每周定时执行一次规则:
每周执行 0 0 * * 0

5.每月定时执行一次规则:
每月执行 0 0 1 * *

6.每年定时执行一次规则:
每年执行 0 0 1 1 *

其他例子:
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命令。 ]


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

Guess you like

Origin blog.csdn.net/IHBOS/article/details/113529492