Linux-based process and scheduled task management

process

The relationship between program 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

View static process statistics---------ps

There are two uses for ps to view static process statistics

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

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

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: dormant state, which 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.

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

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

View dynamic process information----------top

Insert picture description here

Number of lines content
The first line is the task queue information 14:44:37 ----System time
up 20 min----The system has been running for
1 user----The number of currently logged in users
load average: 0.00, 0.01, 0.05----System load, that is, unit time The number of tasks processed by the internal system. The next three values ​​are the average values ​​from 1 minute, 5 minutes, and 15 minutes ago to the present
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 zombie processes
The third line of CPU information 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 *命令名/命令行

top常用命令:

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

在top动态显示中按M
Insert picture description here

在top动态显示中按1

Insert picture description here

查看进程信息---------pgrep

pgrep根据特定条件查询进程PID信息(常用的)

-l	#显示进程名称(后面必须跟参数,这个最好带上,不然只显示进程号)
-U	#指定进程的有效用户ID

Insert picture description here

pgrep -U xyz和带了pgrep -l -U xyz的对比

Insert picture description here

查看进程树---------pstree

pstree以树形结构列出进程信息(常用选项)

pstree -aup
-a   #显示完整信息
-u   #列出对应用户名
-p   #列出对应PID号

Insert picture description here

带有-u显示对应用户名

Insert picture description here

进程的启动方式

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

Insert picture description here
输出的信息中包括后台任务序号[1] 、PID号2167。

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

进程的前后台调度

Ctrl+Z组合键

  • 将当前进程挂起,即调入后台并停止执行

jobs命令

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

fg命令

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

Insert picture description here

终止进程的运行

Ctrl+C组合键
  • 中断正在执行的命令
kill命令、killall命令
  • kill用于终止指定PID号的进程(可以加上-9选项强制终止)
  • killall命令 killall用于终止指定名称相关的所有进程(可以加上-9选项强制终止)

Insert picture description here

pkill命令

作用:根据特定条件终止相应的进程
常用命令选项:

-U:根据进程所属的用户名终止相应进程
-t:根据进程所在的终端终止相应程序

Insert picture description here

Insert picture description here

任务管理

一次性计划任务------at

建立任务格式: at [HH:MM] [yyyy-mm-dd]   #如果后面不跟年月日,那么默认使用当前日期
建立任务格式对应: at [小时:分钟] [年-月-日]
查看任务命令:atq
删除任务:atrm 任务编号

Insert picture description here

计划任务管理-----crontab

crontab命令

  • 按照预先设置的时间周期(分、时、日、月、周)重复执行用户指定的命令操作

  • 属于周期性计划任务。

  • 主要设置文件

    • 全局配置文件,位于文件:/etc/crontab
    • 系统默认的设置,位于目录:/etc/cron.*/
    • 用户定义的设置,位于文件:/var/spool/cron/用户名

管理crontab计划任务

编辑计划任务:crontab -e [-u 用户名]     # -u默认的是当前用户
查看计划任务:crontab -l [-u 用户名]
删除计划任务:crontab -r [-u 用户名]

crontab任务配置的格式

字段 分钟 小时 日期 月份 星期 任务
取值范围 0~59 0~23 1~31 1~12 0~7 执行的命令或脚本

注:星期中0和7都代表星期天

时间数值的特殊表示方法:

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

1、每分钟定时执行一次规则:

每1分钟执行: * /1 * * * * 或者* * * * *
每5分钟执行: * /5 * * *  *  

2. The rule will be executed every hour:

每小时执行: 0 * * * * 或者 0  * / 1* * *
每天上午7点执行:0 7 * * *
每天上午7点10分执行:10 7 * * *

3. The rules are executed regularly every day:

每天执行:0 0 * * *

4. Regularly execute the rules once a week:

 每周执行:0 0 * * 1

5. Regularly execute the rules once a month:

每月执行:0 0 1 * *

6. The rules are regularly executed once a year:

每年执行:0 0 1 1 *

7. Other examples

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执行1s命令[注: 0表示星期天,1表示星期1,以此类推,也可以用英文来表示,sun表示星期天,mon表示星期一等。30 3 10,20 * * ls 每月10号及2o号的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执行一次1s命令。]

Guess you like

Origin blog.csdn.net/shengmodizu/article/details/113539617