Linux_ process and scheduled task management

Process and scheduled task management

1. The relationship between procedures and processes

1. Procedure

  • Executable code and data stored in hard disk, CD-ROM, etc.
  • Statically saved code in the file

2. Process

  • Program code running in CPU and memory

  • Dynamically executed code

  • Parent and child processes
    -------◆Each program can create one or more processes

3. Thread

  • The smallest unit that the operating system can perform operation scheduling, included in the process, is the actual operating unit in the process
  • Multiple threads can be concurrent in a process, and each thread performs a different task

4. The relationship between threads and processes

  • ①: A process can have multiple threads, but at least one thread, a thread can only be active in the address space of one process
  • ②: Resources are allocated to the process, all threads of the same process share all resources
  • ③: The CPU is allocated to the thread, that is, the thread is actually running on the processor
  • ④: Threads need to be coordinated and synchronized during execution, and the threads of different processes must use message communication to achieve synchronization

2. View information process ps aux

  • ps command
  • View static process statistics
    [root@localhost ~]# ps aux
    or
    [root@localhost ~]# ps -aux
a: Display all processes on the terminal, including processes of other users.
in: Indicates the user who lists the process
x: Show all terminal processes

Insert picture description here

  • Statistics process information:
  • [root @ localhost ~] # ps -aux | wc -l

Insert picture description here

Insert picture description hereExplanation of each column:

USER 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 status of the itinerary
D: Uninterruptible sleep state
R: running state
S: in sleep state, can be awakened
T: Stop state, it may be paused in the background or the process is in tracking debugging state
Z: Zombie process, the process has been terminated, but some programs are still in the memory
START: The process was triggered to start time
TIME: The time the process actually uses the CPU to run
COMMAND: Process start command

Three. View process information ps -elf

  • ps command

  • View static process statistics
    [root@localhost ~]# 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
Insert picture description here
Insert picture description here

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

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.
  • But when the child process ends before the parent process, and the parent process does not take back the child process and release the resources occupied by the child process, the child process will become a zombie process at this time.

Four. View process information top

  • top command----------- refresh every few seconds
  • View dynamic process ranking information
  • [root@localhost ~]# top

Insert picture description here

1. The first line is the task queue information
  • 11:06:48 --------- System time
  • up 1:22--------The length of time the system has been running
  • 4 user--------Number of currently logged in users
  • Load average: 0.06, 0.60, 0.48 system load, that is, the number of tasks processed by the system in a unit time, the last three values ​​are the average values ​​from 1 minute, 5 minutes, and 15 minutes ago to the present
2. The second line is the process information
  • Tasks ------------------Total number of processes
  • running-------------------The number of running processes
  • sleeping ------------------The number of sleeping processes
  • stopped-------------------Number of processes stopped
  • zombie---------------------The number of zombie processes
3. The third line is the CPU information
  • us ----------------User occupied
  • sy-----------------Kernel occupation
  • ni------------------Priority scheduling occupation
  • id-------------------Idle CPU, to understand the percentage of idle CPU, mainly see the %id part
  • wa ----------------- I / o etc.
  • hi-------------------Hardware interrupt occupation
  • si-------------------Software interrupt occupation
  • st-------------------Virtualization occupancy
4.第四行为内存的信息
  • total-----------------总交换空间
  • free------------------空闲内存
  • used-----------------已用内存
  • buff/cache ----------物理内存和交换内存的缓冲区总和
5.第五行是 交换空间的信息
  • total------------------------总内存空间
  • free--------------------------空闲内存
  • used-------------------------己用交换空间
  • avail Mem---------------------可用物理空间
6.进程信息区各列解释

Insert picture description here
进程信息区各列解释:

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个数和状态
五.查看进程信息 pgrep
  • pgrep命令
  • 根据特定条件查询进程PID信息
  • [root@localhost ~]# pgrep -| “log”
  • -l:显示进程名,缺省时只输出PID号

Insert picture description here

  • .2538 rsyslogd
  • 2113 mcelog
  • [root@localhost ~]# pgrep -I -U teacher -t tty2
  • -U:指定特定用户,-t:指定终端
  • 27483 bash
  • 27584 vim

Insert picture description here

六. 查看进程树 pstree
  • pstree命令
  • 以树形结构列出进程信息
    [root@localhost ~]# pstree -aup
  • -a:显示完整信息
  • -u:列出对应用户名
  • -p:列出对应PID号

Insert picture description here

  • [root@localhost ~]# pstree -ap teacher
  • 只查属于指定用户的进程数结构
    Insert picture description here
七. 进程的启动方式
1. 手工启动
  • 前台启动:用户输入命令,直接执行程序
  • 后台启动:在命令行尾加入“&”符号

Insert picture description here

[root@localhost ~]# cp /dev/cdrom mycd.iso &
######### 输出信息中包括后台任务序号、PID号

Insert picture description here

2.调度启动
  • 使用at命令,设置一次性计划任务
  • 使用crontab命令,设置周期性计划任务
八. 进程的前后台调度
1. Ctrl+Z组合键
  • 将当前进程挂起,即调入后台并停止执行

Insert picture description here

2. jobs命令
  • jobs -l
  • 查看处于后台的任务列表

Insert picture description here

3. fg命令
  • 将后台进程恢复到前台运行,可指定任务序号
  • [root@localhost ~]# jobs
  • [1]- Stopped . cp /dev/cdrom mycd.iso
  • [2]+ Stopped top
  • [root@localhost ~]# fg 1

Insert picture description here

九. 终止进程的运行
1.Ctrl+C组合键
  • 中断正在执行的命令

Insert picture description here

2. kill、killall命令
  • kill用于终止指定PID号的进程
  • killall用于终止指定名称相关的所有进程
  • -9选项用于强制终止

Insert picture description hereInsert picture description here

4. pkill命令
  • 根据特定条件终止相应的进程
  • 常用命令选项
  • ---- -U:根据进程所属的用户名终止相应进程
  • ---- -t:根据进程所在的终端终止相应进程
  • [root@localhost ~]# pgrep -I -U “teacher”
  • 3045 bash
  • [root@localhost ~]# pkill -9 -U " teacher"
  • [root@localhost ~]# pgrep -l -U " teacher" ####查看
十.计划任务管理 _____一次性计划任务 at
  • at命令

  • 一次性计划任务

  • at [HH:MM] [yyyy-mm-dd]
    -----HH :小时
    ----- MM:分钟
    ----- yyyy:年
    ----- dd:日
    ------不跟年月日,代表今天

  • [root@localhost ~]# date

  • Sun May 7 10:33:13 EDT 2017

  • [root@localhost ~]# at 10:35 2017-05-07

  • at> pgrep U root | WC -1 > /tmp/ps.root
    -----统计root 用户的进程数量
    -------->:把查询到的结果,输入到某个文件中,并且覆盖
    -----------统计root用户的进程数量,输入命令,保存到>…目录中,在按CTRL+D提交任务
    at> < EOT> #### 按CTRL+D提交任务

想在今天的18:41 计划管理任务设定重启
  • at 18:41
  • at>init 6
  • at>< EOT> ####按crtl+D 提交任务,到18:41时候,会重启:
    Insert picture description hereInsert picture description here
    Insert picture description here
查看未执行的任务列表
  • [root@localhost ~]# atq
  • 2 Sun May 7 21:30:00 2017 a root

Insert picture description here

删除第1条任务
  • [root@localhost ~]# atrm 1
  • atrm -后面跟的是想要删除的序号
  • [root@localhost ~]# atq

Insert picture description here

十一. 计划任务管理 __ 预先设定周期计划任务 crontab
crontab命令

-----按照预先设置的时间周期(分、时、日、月、周)重复执行用户指定的命令操作
-------- 属于周期性计划任务
---------主要设置文件
---------------------全局配置文件,位于文件: /etc/crontab
----------------------系统默认的设置,位于目录: /etc/cron.*/
----------------------用户定义的设置,位于文件: /var/spool/cron/用户名

编辑计划任务
  • crontab -e -u 用户名 :
  • -u 缺省时,默认针对当前用户
查看计划任务
  • crontab -l -u 用户名
删除计划任务
  • crontab -r -u 用户名
1. crontab任务配置的格式

Insert picture description here

字段 说明
分钟 取值为从0到59之间的任意整数
小时 取值为从0到23之间的任意整数
日期 取值为从1到31之间的任意整数
月份 取值为从1到12之间的任意整数
星期 取值为从0到7之间的任意整数,0或7代表星期日
命令 要执行的命令或程序脚本——绝对路径来表示
绝对路径:/usr/bin/ls来表示
2. 时间数值的特殊表示方法
* 表示该范围内的任意时间
表示间隔的多个不连续时间点
- 表示一个连续的时间范围
/ 指定间隔的时间频率
举例:
  • 0 17 * * 1-5 ------------------------表示为:每周一到周五的17:00
  • 30 8 * * 1,3,5 ------------------表示为:每周1,3,5的8:30
  • 0 8-18/2 * * * --------------------------表示为:8-18点 每2小时
  • 0 * */3 * *--------------------------------表示为:每3天
  • */1 * * * * ---------------------------------表示为:每分钟执行一次
  • */5 * * * * ---------------------------------表示为:每5分钟执行一次
  • 0 /1 * * ----------------------------------表示为:每小时执行一次
  • 0 7 * * * ----------------------------------表示为:每天7点执行一次
  • 0 0 * * * ----------------------------------表示为:每天执行一次
  • 0 0 * * 1-----------------------------------表示为:每周执行一次
  • 0 0 1 * *------------------------------------表示为:每月执行一次
  • 0 0 1 1 * ------------------------------------表示为:每年执行一次
  • 5 * * * *---------------------------------------表示为:每小时的第五分钟执行一次
  • 30 7 8 * * ------------------------------------表示为:每个月的8号7:30执行一次
  • 30 5 6 8 * -------------------------------------表示为:每年8月6号的5:30执行一次
  • 30 6 * * 0--------------------------------------- Expressed as: every Sunday Execute once at 6:30
  • 30 3 10, 20 * * -------------------------- Expressed as: every month on the 10th and 20th at 3:30
  • 25 8-11 * * * ---------------------------------- Expressed as: 8 to 11 every day Execute once at 25 o'clock
  • */15 * * * * -----------------------------------------denoted as : Execute every 15 minutes
  • 30 6 */10 * * ---------------------------------- Expressed as: every 10 every month Execute once every day at 6:30

Insert picture description here

Insert picture description here

echo '30 7 * * 6 /usr/bin/systemctl httpd restart '>> /var/spool/cron/root

  • Output sentence, 7:30 every 6th of the week
  • systemctl: absolute path
    Insert picture description here
  • /var/spool/cron/root: file settings customized for users
  • Only the root user executes the usr/bin/systemctl httpd restart command to restart the service at 7:30 every week
  •  >:把输出的内容,输出到文件当中,如果文件中有内容,则会**覆盖**
    
  •  >>  :把输出的内容,输出到文件中,如果文件中有内容,**不会覆盖**,在文件末尾行
    添加 echo '30 7 * * 6 /usr/bin/systemctl httpd restart ' 内容
    

Guess you like

Origin blog.csdn.net/Wsxyi/article/details/113743693