Linux system to view the process and task management plans

 it little friends Hello everyone, this gives us is to manage the Linux operating system processes and scheduled tasks, first of all we all know that the program is stored in an external storage medium (such as a hard disk, CD-ROM) in the executable machine static collection of code and data, and the process is a computer program executed in a dynamic state of the CPU and memory, the Linux system, after each program start can create one or more processes.
For example: to provide Web services httpd program when there are a large number of users simultaneously access the Web page, httpd program may create multiple processes to provide services. So then I'll tell you how in Linux from the following points of view CentOS 7 system is process information and control processes.
1, view the process
2, the control process
3, at a one-time task settings
4, crontab periodic task set

First, we look at the relationship between the procedures and processes:

program

  • Save, executable code and data in a medium such as an optical disk
  • Static code stored
    process:
  • Program code running in CPU and memory
  • Dynamic code execution
  • Parent and the child: Every process can create one or more processes
    are here to supplement a knowledge point: the relationship between threads and processes
  • Each process contains a plurality of threads, the thread is a collection of processes
    specific relationship can be shown in FIG follows:
    Linux system to view the process and task management plans

A. Check process

ps command (see static process statistics Statistic Processes)
l is the most commonly used Linux system process viewer tool, mainly used to display a static snapshot contains complete information on the processes currently running through the different command options, you can selectively View process information
ps -aux (in the form of a simple list will show the process information):

a: See among all processes system
u: on behalf of the specified user
x: the user process at all terminals (TTY terminal, ps / 0 remote terminal)

If we want to see the processes running under the current system the root user which, enter: ps auxget the following interface:
Linux system to view the process and task management plans
us about the meaning of each field description:

1, USER: User
2, PID: process ID (ls -l | grep init can see the soft link init, process 1 is init)
3, the CPU%: the CPU utilization process
4,% MEM: memory usage process the rate of
5, VSZ: virtual memory usage
6, RSS: physical memory footprint
7, TTY: terminal (on behalf of the unknown, mostly local?)
8, STAT: the current process state, where
"S": interruptible sleeping processes
"D": uninterruptible sleeping processes
"s": parent process
"<": Representative high-priority
"N": Representative low priority
"R & lt": process representing the running
"the I ': multi-linear process
," the Z': zombie
"+ ": foreground process
9, sTART: start time
10, tIME: the process takes a total time of cpu
name of the process: 11, COMMAND

ps -elf (long format display system will process information):

e: all current process information within the system
l: Use a long format process information
f: use the full format process information

Linux system to view the process and task management plans

Us about the meaning of each field and are not the same as described above:

1、PPID:当前进程的父进程
2、PIR:用户态(人可以进行交互额)优先级
3、NI:内核态优先级(-20~19,数值越低优先级越高)
4、ADDR:-代表正在运行
5、SZ:占用swap交换分区的容量
6、WCHAN:当前进程内核态的名称
7、CMD:命令名称

top命令(查看进程动态信息)
 使用ps命令查看到的是一个静态的进程信息,并不能连续的反馈出当前进程的运行状态,若希望以动态刷新的方式显示各进程的状态信息,可以使用top命令,该命令将会在当前终端全屏交互式的界面显示进程排名,及时跟踪包括CUP、内存等系统资源占用情况,默认情况下每隔3秒刷新一次,其作用类似于windows系统中的“任务管理器”。
输入top得到如下界面:
Linux system to view the process and task management plans
表中的信息表述含义如下:

1、total:总进程数
2、running:正在运行的进程数
3、sleeping:休眠的进程数、
4、stopped:中止的进程数
5、zombie:僵死无响应的进程数
6、%Cpu(s):CPU占用信息,其中:
“us”:用户占用
“sy”:内核占用
“ni”:内核调度优先级
“id”:空闲空间占用
“wa”:IO读写占用
“hi”:硬线程占用
“si”:软线程占用
“st”:虚拟化占用
7、Men:内存占用信息,其中:
“total”:总内存空间
“free”:空闲内存
“used”:已用内存
“buff/cahce”:缓存占用
8、Swap:交换空间占用,其中:
“total”:总交换空间
“free”:空闲交换空间
“used”:已用交换空间
“avail men”:物理占用

pgrep命令(查询进程信息):
 当使用ps命令查询某个进程新的PID信息时,往往需要配合grep命令对输出结果进行过滤,但这样使用非常不方便,而pgrep命令则正是用来查询特定进程信息的专用工具,使用pgrep命令可以根据进程的名称、运行该进程的用户、进城所在的终端等多种属性查询特定进程的PID号。
常见的选项:
-l:既显示PID也显示进程名
Linux system to view the process and task management plans
-U:查看指定用户的进程,此处例如zhangsan
Linux system to view the process and task management plans

此处需要注意的点:当查看指定用户的进程时,需要在虚拟机中先用zhangsan用户登录,因为只有登陆了系统才会加载进程,否侧是无法查看到的!

pstree命令(查看进程树):
 pstree命令可以输出Linux系统中各进程的树形结构,更加直观的判断出各进程之间的相互关系(父、子进程)。
常用选项:

-p:同时列出相对应的PID号
-u:列出相对应的用户名
-a:列出完整的命令信息

输入petree -aup得到当前系统的进程数,包括各进程对应的PID号、用户名、完整命令等信息,如下图所示:
Linux system to view the process and task management plans

二.控制进程

进程的启动方式
1、手工启动
前台启动:用户输入命令,直接执行程序
后台启动:在命令行尾加入按&符号
后台启动举例:
当使用cp命令从光盘中制作镜像文件时,由于需要复制的数据较多,耗时较长,因此可以结合&符号将复制操作放到后台运行,以便于用户可以继续执行其他命令操作,执行过程和结果如下:

[root@localhost~]#cp /dev/cdrom mycd/iso &
[1]28454

2、调度启动
使用at命令,是指一次性计划任务
使用crontab命令,设置周期性计划任务

进程的前后台调度
1、Ctrl+Z组合键:
 当Linux系统中的命令正在前台执行时,按Ctrl+Z组合键可以将当前进程挂起(调入后台并停止执行),这种操作在需要暂停当前进程并进行其他操作时特别有用。
2、jobs命令:
 需要查看当前终端中在后台运行的进程任务时,可以使用jobs命令,结合“-l”选项可以同时显示处该进程对应的PID号,在jobs命令的输出结果中,每一行记录对应一个后台进程的状态信息,首行的数字表示该进程在后台的任务编号。
3、fg命令:
 将后台进程恢复到前台运行,可指定任务序号,示例如下:

[root@localhost~]#jobs
[1]- Stopped cp/dev/cdrom mycd.iso
[2]+ Stopped top
[root@localhost~]#fg 1

终止进程的运行:
1、Ctrl+C组合键
中断正在执行的命令
2、killkillall命令(跟号码,具有唯一性)
常用选项:

kill:用于终止指定PID号的进程
killall:用于终止指定名称的所有进程
-9:选项用于强制终止

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

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

例如以下形式:

[root@localhost~]#pgrep -l -U“hackli”
[root@localhost~]#pkill -9 -U“hackli”
[root@localhost~]#pgrep -l -U“hackli”

举例:我们在系统中登录zhangsan用户,进入桌面:
Linux system to view the process and task management plans
如果此时我们想把张三用户踢出,使用Xshell界面登录的root账户下输入:pkill -9 -U zhangsan,敲回车,此时系统会自动跳回开机之后的用户登录界面,zhangsan用户的所有进程全部被终止:
Linux system to view the process and task management plans

三.at一次性任务设置

格式:at [HH:MM] [yyyy-mm-dd]
具体执行操作如下:

1、[root@localhost~]#date(获取系统的当前时间)
2、2019年 08月 24日 星期六 16:08:24 CST
3、[root@localhost~]#at 16:12 2019-08-24(计划性时间要在系统当前时间之后)
4、at>ps aux | wc -l > /opt/ps.txt(确定执行的动作)
5、ctrl+d(提交)
6、atq(查看计划任务)
Linux system to view the process and task management plans
到了计划时间之后输入:ls /opt/查看目录
此时显示计划的文件ps.txt被写入
Linux system to view the process and task management plans

四.crontab周期性任务设置

crontab命令
1.crontab的配置文件和目录

按照预先设置的时间周期(分钟、小时、天……)重复执行用户指定的命令操作
属于周期性计划任务

主要设置文件

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

管理cron计划任务

编辑计划任务:crontab -e [-u 用户名]
查看计划任务:crontab -l [-u 用户名]
删除计划任务:crontab -r [-u 用户名]

定时编辑格式
Linux system to view the process and task management plans
每个字段的说明:

分钟:取值为从0到59之间的任意整数
小时:取值为从0到23之间的任意整数
日期:取值为从1到31之间的任意整数
月份:取值为从1到12之间的任意整数
星期:取值为从0到7之间的任意整数,0或者7代表星期日
命令:要执行的命令或程序脚本

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

*:表示该范围内的任意时间:
,:表示间隔的多个不连续时间点
-:表示一个连续的时间范围
/:指定间隔的时间频率

应用示例:

0 17 * * 1-5 (周一到周五每天17:00)
30 8 * * 1,3,5 (每周一、三、五的8点30分)
0 8-18/2 * * * (8点到18点之间每2个小时)
0 * */3 * * (每3天)

计划性周期任务实验:

Input: cat /etc/crontab(View Profile)
Linux system to view the process and task management plans
Input: crontab -e(as configured into the planned single task list)
by iinput:
Input: 33 16 * * * /usr/bin/cp -r /home /zhangsan /opt/(16:33 pm daily program executed copy zhangsan home directory to opt in)
enter: wqsave and exit
enter: crontab -l(View to have more command execution)
Linux system to view the process and task management plans
input: ls /opt/(at this time there is no directory zhangsan user's home directory)
Linux system to view the process and task management plans
to the time after we verify, enter it again ls /opt/, then you can see zhangsan user's home directory has been copied to the directory opt at
Linux system to view the process and task management plans
this time planned mission statement set executed successfully.

Guess you like

Origin blog.51cto.com/14464303/2433212