Linux process and scheduled task management (illustrated introduction)

1. The relationship between procedures and processes

1. Program
(1) Executable code and data stored in hard disk, CD-ROM and other media
(2) Code stored statically in files

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

2. View and control the process

2.1 View process

Knowing the status of the process in the system is a prerequisite for managing the process. You can view the process status from different angles by using different command tools.

2.1.1 ps command - view static process statistics

Method 1 : ps aux
a: Display all processes on the terminal bai, including processes of other users.
u: Indicates the user who lists the process.
x: Display the progress of all terminals.
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 suspended 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.

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

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 any process is a child process of the process that just ended. If it is, 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.

2.1.2 Top command to view process information

View dynamic process ranking information
Insert picture description here
1) The first line is the task queue information

00:30:57 system time
up 3:35 How long the system has been running
4 users Number of currently logged in users
load average: 0.00 0.01 0.05 System load, that is, the number of tasks processed by the system in a unit time. The following three values ​​are the average values ​​from 1 minute, 5 minutes, and 15 minutes ago to the present

2) The second line of process information

Tasks Total number of processes
running Number of running processes
sleeping Number of sleeping processes
stopped Number of aborted processes
zombie Number of zombie processes

3) 第三行为CPU的信息

us 用户占用
sy 内核占用
ni 优先级调度占用
id 空闲CPU,要了解空闲的CPU百分比,主要看%id部分
wa I/0 等待占用
hi 硬件中断占用
si 软件中断占用
st 虚拟化占用

4)第四行为内存的信息

total 总内存空间
free 空闲内存
used 已用内存
buff/cache 物理内存和交换内存的缓冲区总和

5)第五行为交换空间的信息

total 总交换空间
free 空闲交换空间
used 已用交换空间
avail Mem 可用物理空间

6)进程信息区各列解释:

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

7)top的常用操作

P键 根据CPU使用百分比大小进行排序
M键 根据驻留内存大小进行排序
N键 根据启动时间进行排序
c键 切换显示命令名称和完整命令行
h键 可以获得 top程序的在线帮助信息
k键 根据提示输入指定进程的 PID 号并按 Enter 键终止对应的进程
q键 退出 top 程序
数字1键 显示CPU个数和状态

2.1.3 pgrep命令–查看进程信息

pgrep根据特定条件查询进程PID信息
-l //显示进程名称(后面必须跟参数,这个最好带上“ ”,不然只显示进程号)
Insert picture description here
-U //指定进程的有效用户ID
这边在2 centos7-1中用pgrep -l -U查看进程
Insert picture description here

2.1.4 pstree命令–查看进程树

pstree以树形结构列出进程信息
pstree -aup
常用选项:
-a //显示完整信息
-u //列出对应用户名
-p //列出对应PID号
Insert picture description here

2.2 控制进程

2.2.1 启动进程

1、手工启动

  • 前台启动:用户输入命令,直接执行程序

  • 后台启动:在命令行尾加入“&”符号
    2、调度启动

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

  • 使用crotab命令,设置周期性计划任务
    3、进程的前后台调度

  • Ctrl+Z 组合键
    将当前进程挂起,即调入后台并停止执行

  • jobs命令
    查看处于后台的任务列表
    jobs -l(加上-l显示进程号,某些命令需要进程号)

  • fg命令
    将后台进程恢复到前台运行,可指定任务序号

  • 将后台进程恢复到前台运行,可指定任务序号

jobs
fg 1

4、终止进程的运行

  • Ctrl+C组合键
    中断正在执行的命令
  • kill命令
    kill用于终止指定PID号的进程(可以加上-9选项强制终止)
  • killall命令
    killall用于终止指定名称相关的所有进程(可以加上-9选项强制终止)
  • pkill命令
    作用:根据特定条件终止相应的进程
    常用命令选项:
  • -U:根据进程所属的用户名终止相应进程
  • -t:根据进程所在的终端终止相应程序

3.任务管理

1、一次性计划任务-at
建立任务格式: at [HH:MM] [yyyy-mm-dd] //果后面不跟年月日,那么默认使用当前日期
建立任务格式对应: at [小时:分钟] [年-月-日]
查看任务命令:atq
删除任务:atrm 任务编号
2、计划任务管理-crontab
(1)crontab命令
按照预先设置的时间周期(分、时、日、月、周)重复执行用户指定的命令操作,是一种周期性计划任务。
主要设置文件
全局配置文件,位于文件:/etc/crontab
系统默认的设置,位于目录:/etc/cron.*/
用户定义的设置,位于文件:/var/spool/cron/用户名
(2)管理crontab计划任务
1)编辑计划任务:crontab -e [-u 用户名] //-u默认的是当前用户
2)查看计划任务:crontab -l [-u 用户名]
3)删除计划任务:crontab -r [-u 用户名]
(3)crontab 任务配置的格式
字段:分钟(范围0-59)–小时(0-23)–日期(1-31)–月份(1-12)–星期(0-7,其中0和7都代表星期天)——任务内容设置(执行的命令或脚本)
时间数值的特殊表示方法:

符号 意义
* 表示这个字段的范围内任意的时间
表示间隔的多个不连续时间点
- 表示一个连续的时间范围
/ 指定间隔的时间频率

crontab -e [-u root]
1), the rule is executed every minute:
every 1 minute: */1 * * * * or * * * * *
every 5 minutes: */5 * * * *
2) , Execute the rule
every hour: Execute every hour: 0 * * * * or 0 */1 * * *
Execute at 7 am every day: 0 7 * * *
Execute at 7:10 every day: 10 7 * * *
3 ), the rule is executed regularly
every day : 0 0 * * *
4), the rule is executed once a
week : 0 0 * * 0
5), the rule is
executed every month : 0 0 1 * *
6), the rule
will be executed regularly once a year : 0 0 1 1 will be executed every year *

How to set up periodic tasks:

每个星期6的7点30重启httpd服务
echo '30 7 * * 6 /usr/bin/systemctl httpd restart' >> /var/spool/cron/root

Guess you like

Origin blog.csdn.net/zhangyuebk/article/details/113776759