Process and scheduled task management
View process
Overview of procedures and processes
- 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
- Sub-parent process and child process (each program can create one or more processes)
View process information (ps)
The ps command is to view static process statistics
- Command①: ps aux
Options | Description |
---|---|
a | Display all processes on the terminal, including processes of other users |
u | Indicates the user who lists the process |
x | Show all terminal processes |
- Explanation of each column
name | Description |
---|---|
USER | User of the process |
PID | ID of the process |
%CPU | Percentage of CPU 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. 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 process was triggered to start time |
commander | Process start command |
- Zombie process
- If too many zombie processes are not released, they will occupy related resources such as CPU and memory, and the system will slow down
- 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 to see if there are any No process is a child process of the process that has 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.
- Command ②: ps -elf
Options | Description |
---|---|
-e | Display all process information in the system |
-l | Use long format to display process information |
-f | Use the completed format to display process information |
- Explanation of each column
name | Description |
---|---|
F | System tag assigned by the kernel to the process |
S | The state of the process |
UID | The user who started these processes |
PID | Process ID of the process |
PPID | The process number of the parent process (if the process was started by another process) |
C | CPU utilization in the life cycle of a process |
PRI | |
NI | Modesty value is used to determine priority |
ADDR | The memory address of the process |
SZ total, | If the process is called, the approximate size of the swap space required |
WCHAN | If the process is sleeping, display the name of the system function in sleep |
ESTIMATES | System time when the process started |
TTY | Terminal device when the process starts |
TIME | Cumulative CPU time required to run the process |
CMD | Process start command |
View process information (top)
The top command is to view dynamic process ranking information
- Command: top
- The first line is the task queue information
E.g | top - 11:43:12 up 11 min, 1 user, load average:0.00,0.01,0.0.5 |
---|---|
11:43:12 | system time |
up 11 min | How long the system has been running |
1 user | Number of currently logged in users |
load average:0.00,0.01,0.0.5 | The system load, that is, the number of tasks processed by the system in a unit time, the following three values are 1 minute ago. 5 minutes ago, 15 minutes ago to the current average |
- The second line is the process information
E.g | Tasks: 129 total,1 running,128 sleeping,0 stopped,0 zombie |
---|---|
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 |
- The third line is cpu information
E.g | % Cpu (s): 0.0 us, 15.2 sy, 0.0 ni, 81.3 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st |
---|---|
us | User occupation |
his | Kernel occupation |
ni | Priority scheduling occupation |
id | Idle cpu (to understand the percentage of free cpu, mainly see the %id part) |
wa | i/o waiting to be occupied |
hi | Hardware interrupt occupation |
and | Software interrupt occupation |
st | Virtualization occupancy |
- The fourth line is the memory information
E.g | KiB Mem: 2011765 total,1411487 free,254016 used,316671 buff/cache |
---|---|
total | 总内存空间 |
free | 空闲空间 |
used | 已用内存 |
buff/cache | 物理内存和交换内存的缓冲区总和 |
- 第五行是交换空间的信息
例如 | KiB Swap: 4011765 total,4011765 free,0 used,116671 avail Mem |
---|---|
total | 总内存空间 |
free | 空闲空间 |
used | 已用内存 |
avail Mem | 可用物理内存 |
- top常用命令
命令 | 说明 |
---|---|
P键 | 根据CPU使用百分比大小进行排序 |
M键 | 根据驻留内存大小进行排序 |
N键 | 根据启动时间进行排序 |
c键 | 切换显示命令名称和完整命令行 |
h键 | 可以获得top程序的在线帮助信息 |
k键 | 根据提示输入指定进程的PID号并按Enter键终止对应的进程 |
q键 | 退出top程序 |
数字1键 | 显示CPU个数和状态 |
查看进程信息(pgrep)
pgrep命令是根据特定条件查询进程PID信息
- 命令:pgrep -l -U [用户] -t [终端]
- 例如:pgrep -l -U teacher -t tty2
选项 | 说明 |
---|---|
-l | 显示进程名(默认时只输出PID号) |
-U | 指定特定用户 |
-t | 指定终端 |
查看进程树(pstree)
pstree命令是以树形结构列出进程信息
- 命令:pstree -aup
- 例如:pstree -ap teacher
(只查出属于teacher用户的进程数结构)
选项 | 说明 |
---|---|
-a | 显示完整信息 |
-u | 列出对应用户名 |
-p | 列出对应PID号 |
控制进程
进程的启动方式
- 手工启动
- 前台启动:用户输入命令,直接执行程序
- 后台启动:在命令行尾加入"&“符号
加入”&"符号后输出信息中就会包括后台任务序号、PID号
- 调度启动
- 使用at命令,设置一次性计划任务
- 使用crontab命令,设置周期性计划任务
进程的前后台调度
- Ctrl+Z组合键
- 将当前进程挂起,即调入后台并停止执行
- jobs命令
- jobs [-l]
查看处于后台的任务列表
- fg命令
- 将后台进程恢复到前台运行,可指定任务序号
- jobs [-l]
终止进程的运行
- Ctrl+C组合键
- 中断正在执行的命令
- kill、killall命令
- kill用于终止指定PID号的进程
- killall用于终止指定名称相关的所有进程
- -9选项用于强制终止
- pkill命令
- 根据特定条件终止响应的进程
- 常用命令选项
-U:根据进程所属的用户名终止响应进程
-t:根据进程所在的终端终止相应进程
计划任务管理(at、crontab)
at命令
- at命令是一次性计划任务
- 命令:at [HH:MM] [yyy-mm-dd]
[yyy-mm-dd]不填则默认当天 - 操作步骤
- date
查询当前时间 - at 10:11
- at> reboot
设置当天10:11分重启 - Ctrl+D
提交任务 - atq
查看未执行的任务列表 - atrm 1
删除第1条任务
- date
crontab命令
-
按照预先设置的时间周期(分钟、小时、天、月、周)重复执行用户指定的命令操作
-
crontab属于周期性计划任务
-
主要配置文件
①.全局配置文件,位于文件:/etc/crontab
②.系统默认的设置,位于目录:/etc/cron.*/
③.用户定义的设置,位于文件:/var/spool/cron/用户名 -
编辑计划任务
crontab -e [-u 用户名] -
查看计划任务
crontab -l [-u用户名] -
删除计划任务
crontab -r [-u用户名]
-u默认是针对当前用户 -
acrontab任务配置的格式
例如 | 44 2 10 2 * run_command |
---|---|
分钟 | 取值为0-59之间的任意整数 |
小时 | 取值为0-23之间的任意整数 |
日期 | 取值为1-31之间的任意整数 |
月份 | 取值为1-12之间的任意整数 |
星期 | 取值为0-7之间的任意整数,0或7代表星期日 |
命令 | 要执行的命令或程序脚步 |
另一方法 | echo ‘10 1 * * 5 /usr/bin/systemctl httpd restart’>> /var/spool/cron/root root用户的每周五1:10执行httpd服务的重启命令,并在/var/spool/cron文件末尾行追加相关信息 |
- 时间数值的特殊表示方法
“*”:表示该范围内的任意时间
“,”:表示间隔的多个不连续时间点
“-”:表示一个连续的时间范围
“/”:指定间隔的时间频率 - 示例
0 15 * * 1-5(周一到周五每天15:00)
30 1 * * 3,4,5(每周三、四、五的1:30)
0 5-11/3 * * *(5点-10点之间每3小时)
0 * */5 * *(每5天.)