Process and scheduled task management

1. Procedures and processes

1.1 Procedure

Report the executable code and data in the hard disk, CD-ROM and other media

Statically saved code in the file

1.2 Process

Program code running in CPU and memory

Dynamically executed code

Parent and child processes

Each program creates one or more processes

1.3 The difference between process and thread

1、进程是资源分配最小单位,线程是程序执行的最小单位;

2、进程有自己独立的地址空间,每启动一个进程,系统都会为其分配地址空间,建立数据表来维护代码段、堆栈段和数据段,线程没有独立的地址空间,它使用相同的地址空间共享数据;

3、CPU切换一个线程比切换进程花费小;

4、创建一个线程比进程开销小;

5、线程占用的资源要⽐进程少很多。

6、线程之间通信更方便,同一个进程下,线程共享全局变量,静态变量等数据,进程之间的通信需要以通信的方式(IPC)进行;(但多线程程序处理好同步与互斥是个难点)

7、多进程程序更安全,生命力更强,一个进程死掉不会对另一个进程造成影响(源于有独立的地址空间),多线程程序更不易维护,一个线程死掉,整个进程就死掉了(因为共享地址空间);

8、进程对资源保护要求高,开销大,效率相对较低,线程资源保护要求不高,但开销小,效率高,可频繁切换;

1.3 View process information

1.3.1 ps

View static process statistics

Method 1: ps aux

[root@localhost~]# ps   aux


a:显示终端上的所有进程,包括其他用户的进程

u:表示列出进程的用户

x:显示所有终端的进程

Insert picture description here

USER:进程的用户

PID:进程的ID

%CPU:进程占用的CPU百分比

%MEN:占用的内存的百分比

VSZ:该进程使用的虚拟内存量(KB)

RSS:该进程占用的物理内存量(KB)

TTY:启动进程的终端名。不是从终端启动的进程则显示为?

STAT:该进程的状态。D:不可中断的休眠状态。R:正在运行状态。S:处于休眠状态,可被唤醒。T:停滞状态,可能是在后台暂停后进程处于跟踪调试状态。Z:僵尸进程,进程已经终止,但是部分程序还在内存中。

START:该进程被触发启动时间。

TIME:该进程实际使用CPU运行时间

COMMAND:进程的启动命令。

Explanation:
(1) Zombie process: a process ends, but if the parent process of the process has ended, then the process will not become a zombie process, because when each process ends, the system will scan the current system For all processes, see if any process is a child process of the process that has just ended. If so, 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) tty terminal: CentOS7 system, tty1 represents the graphical interface, tty2-6 represents the text interface, you can use Ctrl+Alt+F1-F6 to switch.

pts indicates that it is connected with a remote tool, such as xshell. The number after it represents the time sequence of login. The smaller the number, the earlier the login.

Method Two:

[root@localhost~]# ps -elf

-e:显示系统内的所有进程信息

-l:使用长格式显示进程信息

-f:使用完整的格式显示进程信息

### Diagram

F:内核分配给进程的系统标记
S:进程的状态
UID:启动这些进程的用户
PID:进程的进程ID
PPID:父进程的进程号(如果该进程是由另一个进程启动的)
C:进程生命周期中的CPU录用率
PRI:进程的优先级(越大的数字代表越低的优先级)
NI:谦让度值用来参与决定优先级
ADDR:进程的内存地址
SZ:假如进程被换出,所需要的交换空间的大致大小
WCHAN:若该进程在睡眠,则显示睡眠中的系统函数名
STIME:进程启动时的终端设备
TTY:进程启动时的系统时间
TIME:运行进程需要的累计CPU时间
CMD:进程的启动命令

1.3.2 top

View dynamic process ranking information

[root@localhost~]#  top

### Diagram

The first line is the task queue information

14:27:34    系统时间
up   9 min    系统已运行时长
1  user       当前登陆用户数

load  average  :0.00,0.02,0.04
系统负载,即单位时间内系统处理的任务数,后面三个数值分别是1分钟、5分钟、15分钟前到现在的平均值。

The second line is the process information

Tasks:总进程数
running:正在运行的进程数
sleeping:休眠的进程数
stopped:中止的进程数
zombie:僵尸进程数

The third line is the CPU information

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

The fourth line is memory information

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

The fifth line of information exchange space

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

Explanation of each column in the process information area

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

top commonly used commands

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

1.3.3 pgerp

Query process PID information according to specific conditions

Insert picture description here

1.3.4 List process information in a tree structure


Insert picture description here

1.4 How to start the process

1.4.1 Manual start

(1) Start at the foreground: the user enters a command and executes the program directly

(2) Background startup: add "&" symbol at the end of the command line

### Diagram

1.4.2 Transfer start

Use the at command to set up a one-time scheduled task

Use crontab command to set up periodic scheduled tasks

1.5 Process scheduling

1.5.1 Ctrl+Z key combination

Suspend the current process, that is, transfer to the background and stop execution

1.5.2 jobs command

View the list of tasks in the background

jobs   [-l]

1.5.3 fg command

Restore the background process to the foreground, you can specify the task number

[root@localhost~]# jobs

[1]- Stopped          cp  /dev/cdrom/  mycd.iso

[2]+ Stopped         top

[root@localhost~]# fg  1 (恢复[1]命令到前台)

1.6 Terminate the operation of the process

1.6.1 Ctrl+C key combination

Interrupt the command being executed

1.6.2 kill, killall commands

kill:终止指定PID号的进程

killall:终止指定名称相关的所有进程

-9:强制终止

1.6.3 pkill command

Terminate the corresponding process according to specific conditions

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

-t:根据进程所在的终端终止相应进程

Example

[root@localhost~]#pgrep    -l  -U  "teacher"

3045  bash

[root@localhost~]#pkill   -9  -U  "teacher"

[root@localhost~]#pgrep   -l   -U   "teacher"

2. Planned task management

2.1 at command

One-time scheduled task

at [HH:MM] [yyyy-mm-dd]

### Diagram
Insert picture description here

Example: Automatically shut down the system at 21:30 of the day

### Diagram

2.2 crontab command

Repeat the command operation specified by the user according to the preset time period (minute, hour, day, month, week)

Periodic scheduled task

2.2.1 Main configuration files:

(1)全局配置文件,位于文件:/etc/crontab

(2)系统默认的设置,位于目录:/etc/cron.*/

(3)用户定义的设置,位于文件:/var/spool/cron/用户名

2.2.2 Manage crontab scheduled tasks

1. Edit the scheduled task (-u defaults to the current user by default)

crontab  -e  [-u   用户名]

2. View scheduled tasks

crontab   -l  [-u  用户名]

3. Delete the scheduled task

crontab  -r  [-u  用户名]

2.2.3 Format of crontab task configuration

All values ​​are integers

minute hour date month week command
0-59 0-23 1-31 1-12 0-7 (0/7 all refer to Sunday) Command or program script

Special representation method of time value

symbol significance Example
* Any time in the range 0 17 * * 1-5 (17:00 every day from Monday to Friday)
Multiple discrete time points 30 8 * * 1, 3, 5 (Every Monday, Wednesday and Friday at 8:30)
- A continuous time range 0 8-18/2 * * * (every two hours between 8 o'clock and 18 o'clock)
/ Specify the time frequency of the interval 0 * */3 * * (every three days)

Insert picture description here

Insert picture description here

Method 2:
Add
echo '30 7 * * 6 /usr/bin/systemctl httpd restart' >> /var/spool/cron/root
Insert picture description here

Guess you like

Origin blog.csdn.net/IvyXYW/article/details/110430970