Process plan management

1. View and manage processes

  • Kernel functions: process management, memory management, file system, network functions, drivers, security functions, etc. For all operating systems, there are basic functions
  • What is the procedure:
    • Executable code and data stored in hard disk, CD, etc.
    • Code saved statically in the file
  • what is a process
    • Program code that runs on the CPU and memory
    • execute code dynamically
    • Parent, child process (each program can create one or more processes)
    • A process is a program that is being executed
  • The difference between process and thread:
    resource allocation and scheduling
    • Process is the basic unit of resource competition
    • Thread is the smallest unit of program execution

1.1 View process

1.1.1PS command – view static process statistics

Common options

options Function
a Show all processes on the terminal, including those of other users
u Indicates the user listing the processes
x show all terminal processes

When executing the ps command directly without any options, the processes open in the current user session will be displayed in
basic format;

 ps  aux     基本格式
 ps  aux |  wc   -l    结合管道符号查询具体的数量
  • View current system processes
    insert image description here
  • View current progress
    insert image description here
name describe
USER The name of the user account that started the process
PID PID of the process
%CPU The percentage of CPU used by the process
%MEM percentage of memory used
VSZ Amount of virtualized memory used by the process
RSS Amount of physical memory used by the process
TTY The name of the terminal that started the process, or the process that was not started from a terminal is displayed as
STAT status of the process
START The process was falsely triggered to start time
TIME The time the process actually used the CPU to run
COMMAND process start command
  • stat:
    - D: Dormant state that cannot be awakened
    - R: Running state
    - S: Dormant state
    - T: Stopped state
    - Z: Zombie process, the process has terminated, but some programs are still in memory, but its parent The process was unable to gracefully terminate it
  • Zombie process: Zombie process is when the child process ends before the parent process, and the second parent process does not recycle the child process and release the resources occupied by the child process. At this time, the child process will become a zombie process. If the parent process exits first, the child process will be killed. init takes over, and after the child process exits, init will reclaim the closed resources it occupies

1.1.2ps -elf command – view static process statistics

options Function
-e Display all process information in the system
-l Use long format to display process information
-f Display process information in full format
  • Display the current system process (more information than ps aux display)
    insert image description here
name representative meaning
F System flags assigned by the kernel to processes
s state of the process
UID start these processes
PID The process ID of the process, each process has a unique PID
PPID The process ID of the parent process
C CPU utilization over process lifetime
AT process priority
IN The humility value is used to participate in determining priorities
ADDR memory address of the process
sz Approximate size of space required
ESTIMATIONS System time when the process started
TTY Terminal device when the process starts
CMD process start command

1.1.3 grep filter query

Due to the large number of processes running in the system, it is necessary to query the information of a process by combining pipeline operations and grep commands for filtering. For example, do the following to filter out process information that contains "bash"
insert image description here
insert image description here

1.1.4 top command – dynamically view process information

  • Dynamic view process
    insert image description here
  • Sort the queried processes according to the memory ratio, and press shift+m ​​after the top command
    insert image description here
  • View the current process
    insert image description here
    The first line: task queue information
19:11:05 system time
up 1:03 How long the system has been running
2 users Currently logged in users
load average: 0.00,0.01,0.05 Average load, that is, the number of tasks processed by the system per unit time, and the last three values ​​are the average values ​​from 1 minute, 5 minutes, and 15 minutes ago to the present

The second line: system process information

Tasks total number of processes
running 正在运行的进程数
sleeping、 休眠的进程数
stopped 中止的进程数
zombie 僵死的进程数

第三行:CPU占用信息

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

第四行内存占用信息

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

第五行:交换空间swap占用信息

total 总交换内存
free 空闲交换内存
used 已用交换内存
avail Mem 可用物理空间
  • top常用命令
命令 代表意思
P 键 根据CPU使用百分比大小进行排序
M键 根据驻留内存大小进行排序
N键 根据启动时间进行排序
c键 切换显示命令名称和完整命令行
h键 可以获得top程序的在线帮助信息
q键 退出top程序
数字键1 显示CPU个数状态

1.1.5pstree命令-- 查看进程树

pstree命令可以查看当前系统的进程树,包括各个进程对应的pid号,用户名,完整命令等信息

insert image description here

选项 代表意思
-A 各进程树之间连接以ASCLL字符来连接
-u 各进程树之间的连接以Unicode字符来连接,在某些终端界面下可能会有错误
-p 同时列出每个进程的pid
-u 同时列出每个进程的所属账号名称

1.2控制进程方式

进程启动方式为手工启动和调度启动

1.2.1手动启动

  • 前台启动:用户在输入命令,直接执行的程序
  • 后台启动:在命令行尾加入“&”符号
    当使用cp命令从光盘中制作镜像文件时,由于需要复制的数据较多,耗时较长,因此可结合“&”符号将复制操作放到后天运行,以便用户可以继续执行其他命令操作

insert image description here

1.2.2进程的前后台调度

  • ctrl+Z组合键:将当前进程挂起,即调入后台并停止执行
  • jobs命令:查看处于后台的任务列表
  • fg命令:将后天进程恢复到前台运行,可指定任务序列号insert image description here

1.2.3终止程序的运行

  • ctrl+c组合键:中断正在执行的命令
    insert image description here
  • kill,kallall命令
    • kill用于终止指定pid号
    • killall用于终止指定名称相关的所有进程
    • -9选项用于强制执行
      insert image description here
      insert image description here
  • pkill命令:根据特定条件终止相应的进程
-u 根据进程所属的用户名终止相应的进程
-t 根据进程所在的终端终止相应进程

二.计划管理

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

  • at命令一次性计划任务
    insert image description here

2.2使用crontab命令,设置周期性计划任务

2.2.1crontab命令简介

  • crontab命令
    • 按照预先设置的时间周期重复执行用户的命令操作
    • 属于周期性计划任务
    • 服务脚本名称
  • 主要设置文件
    • 全局配置文件,位于文件:/etc/crontab
    • 系统默认的设置,位于目录:/etc/cron.*/
    • 用户定义的设置,位于文件:/var/spoolcron用户名
      insert image description here

2.2.2使用crontab命令管理用户计划任务

编辑计划任务
crontab -e [-u 用户名] #-u缺省时默认是针对当前用户,只有超级用户才可以用-u

查看计划任务
crontab -l [-u 用户名]

删除计划任务
crontab -r [-u 用户名]

  • 时间数值的特殊标识方法:
特殊字符 说明
*(星号) 代表任何时刻都接受的意思。
,(逗号) 代表分隔时段的意思
-(减号) 代表一段时间范围内
/n(斜杠) n代表数字,代表每隔n单位间隔的意思
代表意义 分钟 小时 日期 月份 命令
数字范围 0~59 0~23 1~31 1~12 0~7 需要执行的命令

示例:0 17 * * 1-5 每周一到周五17;00

  • crontab计划任务的配置模式
    时间周期设置 任务内容设置
    命令一定要用绝对路径,例如cp命令,要用/usr/bin/cp,可以使用which查看命令的绝对路径

insert image description here
insert image description here
insert image description here

三、总结

1、掌握程序、进程、线程

2. The command to view the process: ps aux or ps -elf; top; pgrep; pstree;

3. How to check the child process? pstree ap or cd /stat in /proc/numbers file

4. Start the process: start in the foreground; start in the background (add & at the end of the command)
to view the background task list: jobs
call the background process: fg task list number

ctrl+z hang
kill -9 process number PID
pkill -9 process number PID
killall all processes

at hour: minute year-month-day
command: ctrl D to save and run

atq serial number
atrm + serial number

crontab -e -u user (do not write user default current user)
crontab -l -u user
crontab -r -u user

Format: Time-sharing day, month and week If you forget, you can check cat /etc/crontab

* represents any time without interval, such as 1, 2, 4 can be every other week

— continuous time, eg: 1-7
/ frequency of intervals 1/* every minute 1/* every other hour 1/* every other day 1/* every other month

Guess you like

Origin blog.csdn.net/fyb012811/article/details/131988536