linux daily operation and maintenance (a)

- Process Management ps, top

- Task Scheduler crontab

Process Management

Concept: the process is a separate program in its own virtual address space to run.

Processes and procedures difference: the program is just a static set of commands, not the operation of the system resources; and the process is a likely change at any time, dynamic, using the system resources to run the program. A program can start multiple processes. And processes in comparison. Job is a series of commands executed in a certain order. A simple command may involve multiple processes, especially when using pipes and redirection. The process has the following characteristics:

  • Dynamics: the essence of the process is executed once the program is in the process of multi-channel programming system, the initial process is dynamic, dynamic extinction.
  • Concurrency: any process can be executed concurrently along with other processes.
  • Independence: the process is a basic unit can operate independently, but also the system of allocation of resources and scheduling of independent units.
  • Asynchronous: As mutual restraint between processes, the process is carried out with a discontinuity, that is, the process by independent, unpredictable pace to move forward.
  • Structure: the process control block of three parts by a program, data and processes.
  • Different processes can comprise a plurality of the same program: a program in a different data set constitute a different process, different results can be obtained; however, during execution, a program change can not occur.

In linux system consists of three processes:

  • Interactive process: a shell and started the process control, interactive process running in the foreground can also be run in the background.
  • Batch process: regardless of the terminal, arrange a series of processes at a specified time to complete.
  • Daemon: start at boot the system to perform real-time operating system tasks, such as crond, rsyslogd, named and so on.
View system processes
  1. ps Command syntax: ps [options]
Options meaning
-A Show all the process
-N Select all processes except those specified criteria symbol
-a Showing negative session leaders and processes not all processes associated with the terminal
-d Show all the process (exclude the session leader)
-e Show all the process
T Show all processes in the current terminal
a All W / tty, including other users
r Display process runs only in
x Treatment w / o control ttys
-c Show different scheduling information for the -l option
c When listing process, showing each process real command name, and does not include a path identifier, parameter or resident services
-C <command name> Press the command name display process
-G <real GID of the user or group name> Show real progress by group GID or group name
-U <user real UID or user name> According to the real user UID or user name display process
-g <group name> Select the session or a valid group name display process
-p <process ID> Process by process id display
-s <Session ID> Displays the session ID of the process.
-t <Terminal> Press the terminal display process
-u <valid user name or user UID> By a valid user UID display process
U <user name> Display belonging to the user process
t <Terminal> Press the terminal display process
-f Display UID, PPID, C and STIME field
-j or j The display format using job control process
s Adoption process signal format process
v Processes to display virtual memory format
-l or l Using the procedure detailed display format
in To display user-oriented format process
p <process ID> Display specified process ID of the process, and the same effect as the -p option, just in different formats list
L List information output fields
f The results show a tree with ASCII characters, expressing the relationship between processes.
r Show only the termination of the process of
e When listing process, display environment variables used by each process
-w some w Press wide format display output
-u Users print format, display start time username and processes
-x Process display without control terminal
-t <terminal ID> Specifies the number of terminal display process
n With figures to show USER and WCHAN field
h Does not display the title bar
-H Display tree structure, the relationship between the representation process
-m or m Display thread behind the process
-Y When used with the -l option is not displayed F (flag) output field, and to replace the field ADDR field RSS

Examples: View crond process is running

[root@localhost ~]# ps -ef|grep crond
root       6571      1  0 03:04 ? 00:00:00 /usr/sbin/crond -n root 7457 7383 0 03:28 pts/0 00:00:00 grep --color=auto crond 

Show all processes without a console terminal, and displays a user name and start time process.

[root@localhost ~]# ps -aux

Display process under tty1 terminal

[root@localhost ~]# ps -t tty1
   PID TTY          TIME CMD
  6572 tty1     00:00:00 agetty 
  1. The top command: displays currently running processes and important information about them including their memory and CPU usage. Use the q key to exit the top command

Command Syntax: top [options]

Options meaning
-b Using batch mode
-c When the column process, display the full command for each process, including the command name, path and parameters
-d <seconds between> Interval execution state monitoring process, calculated in units of seconds.
-i Ignore idle or has become a Zombie process
-n <executions> Set the number of update monitoring information
-S 使用累计时间模式
-u<用户名或有效用户UID> 仅监控指定有效用户UID或用户名匹配的进程
-p<进程PID> 仅监视指定进程的ID的进程
-U<用户名或用户UID> 仅监视指定用户UID或用户名匹配的进程

例子:只显示root用户的进程

[root@localhost ~]# top -u root
kill命令杀死进程

命令语法:kill [选项] [进程号]

选项 含义
-s<信号> 指定发送的信号
-l 显示信号名称的列表,一共62个

例如:强制杀死进程号为6571的进程

[root@localhost ~]# ps -ef|grep crond
root       6571      1  0 03:04 ? 00:00:00 /usr/sbin/crond -n root 7534 7383 0 03:48 pts/0 00:00:00 grep --color=auto crond [root@localhost ~]# kill -9 6571 [root@localhost ~]# ps -ef|grep crond root 7536 7383 0 03:49 pts/0 00:00:00 grep --color=auto crond 
任务计划:在固定的时间上触发某个作业,就需要创建任务计划,按时执行该作业。使用cron实现任务计划可以通过修改/etc/crontab文件以及使用crontab命令实现,其结果是一样的。
  1. 安装crontabs和cronie软件包
[root@localhost ~]# yum -y install vixie-cron
  1. 控制crond服务 使用以下命令启动crond服务
[root@localhost ~]# systemctl start crond.service

使用以下命令在重新引导系统时自动启动crond服务

[root@localhost ~]# systemctl enable crond.service
[root@localhost ~]# systemctl is-enabled crond.service
enabled
  1. /etc/crontab文件是cron的默认配置文件,内容如下
[root@localhost ~]# cat /etc/crontab
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin MAILTO=root # For details see man 4 crontabs # Example of job definition: # .---------------- minute (0 - 59) # | .------------- hour (0 - 23) # | | .---------- day of month (1 - 31) # | | | .------- month (1 - 12) OR jan,feb,mar,apr ... # | | | | .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat # | | | | | # * * * * * user-name command to be executed 

其中#号开始的行是注释内容,不会被处理。

前面3行是用来配置cron任务运行环境的变量。/etc/crontab文件中的每一行都表示一项任务,它的格式如下

minute     hour      day      month     day of week   user-name command 
内容 描述
minute 分钟,0~59的任意整数
hour 小时,0~23的任意整数
day 日期,1~31的任意整数(如果指定了月份,则必须是该月份的有效日期)
month 月份,1~12的任意整数(或使用月份的英文简写入Jan、Feb等)
day of week 星期,0~7的任意整数,这里的0或7代表星期日(或使用星期的英文简写入Sun、Mon等)
user-name 执行命令的用户
Command 要执行的命令或者是自己编写的脚本

/etc、crontab文件中可以下表的时间格式

时间格式 描述
* 可以用来代表所有有效的值。如月份值中的型号意味着在满足其他制约条件后每月都执行该命令
- 指定一个整数范围。比如1-4意味着整数1、2、3、4
, 指定隔开的一系列值指定一个列表。比如3,4,6,8表明这四个指定的整数。
/ 可以用来指定间隔频率。在范围后加上/<integer>意味着在范围内可以跳过integer。如"0-59/2"可以用来在分钟字段上定义时间间隔为两分钟。间隔频率值还可以和星号一起使用,如"*/3"的值可以用在月份字段中表示每3个月运行一次任务。
  1. /etc/crontab文件配置举例 在每天晚上的21:30执行/root、backup.sh
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
30 21 * * * root /root/backup.sh 

在每星期六、星期日的1:20执行一个find命令,查找相应的文件

20 1  * * 6,0 root /bin/find / -name core -exec rm {} \; 
  1. /etc/cron/d目录:在/etc/cron.d目录中创建文件(该目录下所有文和/etc/crontab文件使用一样的配置语法)来实现。例如:
[root@localhost ~]# ls /etc/cron.d
0hourly
[root@localhost ~]# cat /etc/cron.d/0hourly
# Run the hourly jobs SHELL=/bin/bash PATH=/sbin:/bin:/usr/sbin:/usr/bin MAILTO=root 01 * * * * root run-parts /etc/cron.hourly 
使用crontab命令实现任务计划
  1. cronta命令简介 root以外的用户可以使用crontab命令配置cron任务。所有用户定义的crontab都被保存在/varspool/cron目录中,并使用创建它们的用户身份来执行。
  • 以某位用户身份一个crontab项目
  • 以该用户身份登录
  • 然后输入crontab -e命令,就进入编辑器编辑该用户的crontab了。(该编辑器是由VISUAL或EDITOR环境变量指定的,文件格式和/etc/crontab相同)
  • 编辑成功保存后,该文件会根据用户名被保存在/var/spool/cron/<username>文件中。
  • crond守护进程每分钟都检查/etc/crontab文件、/etc/cron.d目录以及/var/spool/cron目录中的改变。如果发现改变,它们就会被载入内存。
  1. 命令语法:crontab [选项] crontab [选项] [文件] 选项|含义 ---|--- -u<用户名>|用户名,如果使用自己的用户名登录,就不需要使用该选项 -e|编辑用户的crontab -l|列出用户的crontab中的内容 -r|删除用户的crontab -i|删除用户的crontab前进行提示

  2. 创建crontab 例子:以用户zhangsan登录系统,创建crontab

[root@localhost ~]# su - wangwu
[wangwu@localhost ~]$ date
Sat Jul 27 05:41:34 EDT 2019 

使用crontab -e命令打开vi编辑器,编辑成功后查看,切换为root用户查看(普通用户没有权限打开该文件)

[wangwu@localhost ~]$ su - root
Password: 
Last login: Sat Jul 27 05:38:10 EDT 2019 from 192.168.64.1 on pts/0 [root@localhost ~]# cat /var/spool/cron/wangwu 
  1. 编辑crontab过程中可以在每个条目指示加一条注释,这样就可以知道它的功能、运行时间,更为重要的是指定这是哪位用户的作业。
  2. 列出crontab: 以root用户列出wangwu的crontab
[root@localhost ~]# crontab -u wangwu -l
  1. 删除crontab:删除crontab同时会删除/var/spool/cron目录中指定有用户的文件

例如:以普通用户wangwu删除自己的crontab

[wangwu@localhost ~]$ crontab -r
  1. 恢复丢失的crontab文件(找到系统自动备份的位置/var/spool/cron/wangwu)

命令语法:crontab [文件] 例如:以wangwu登录恢复丢失的crontab文件

[wangwu@localhost ~]$ crontab /home/wangwu/wangwucron

摘自:《Linux实用教程》人民邮电出版社出版发行 於岳编著

Guess you like

Origin www.cnblogs.com/mylive/p/11280236.html