Linux调度进程

一:crond定制计划

1.crond的定义

#  crond是linux下用来周期性执行某种任务或等待处理某些事件的一个守护进程,与windows下的计划任务类似,当完成操作系统安装后,默认会安装此服务工具,并且会自动启动crond进程。
#  crond进程每分钟会定期检查是否有要执行的任务,如果有要执行的任务,则自动执行该任务

2.linux下的crond分类

#1.系统任务调度:系统周期性要执行的工作。例如:写缓存数据到硬盘,日志清理等,
linux系统中的/etc/crontab文件就是任务调度的配置文件
#2.用户任务调度:用户定期要执行的工作。例如:用户数据备份,定时邮件提醒等。用户可以使用crontab工具来制定自己的计划任务。所有用户定义的crontab文件都被保存在/var/spool/cron目录下,其文件名与用户名一致且该目录下的crontab文件不能直接创建或直接修改。 
crontab文件是通过crontab命令创建的。

3.crontab命令的用法

①crontab命令用法:

crontab语法:

crontab  [-u user] [file]
crontab  [-u user] [-e|-l|-r|-i]

crontab命令的常用参数

参数说明
-u user用来设置某个用户的crontab服务。未指定则是设置当前用户的crontab服务
filefile是命令文件的名字,表示将file作为crontab的任务列表文件并载入crontab。如果命令行中没有指定该文件,crontab命令将接收键盘标准输入的命令,并将命令载入到crontab
-e编辑某个用户的crontab文件内容。未指定则是设置当前用户的crontab文件
-l显示某个用户的crontab文件内容。未指定则是显示当前用户的crontab文件
-r从/var/spool/cron目录中删除某个用户的crontab文件。未指定则是删除当前用户的crontab文件
-i在删除用户的crontab文件时给确定提示

②crontab文件的含义
#  使用crontab命令创建的crontab文件都包含6个域。其中前五个域是指定命令被执行的时间,最后一个域是要被执行的命令。每个域之间使用空格或者tab键分割。格式:
minute  hour  dayofmonth  mounthofyear   dayofweek   commands
​
  • minute:表示分钟,可以是0~59之间的任何整数

  • hour:表示小时,可以是0~23之间的任何整数

  • dayofmonth:表示日期,可以是1~31之间的任何整数

  • mounthofyear:表示月份,可以是1~12之间的任何整数

  • dayofweek:表示星期几,可以是0~6之间的任何整数,0代表星期日

  • commands:要执行的指令,可以是系统系统指令,也可以是自己编写的脚本文件

③crontab文件中特殊字符含义
  • *(星号):代表所有可能的值。

  • , (逗号):可以用逗号隔开的值指定一个列表范围

  • —(横杠):可以用证书之间的横杠表示一个整数范围

  • /(正斜线):可以用斜线指定时间的间隔频率。例如0~23/2表示每两小时执行一次

④crontab文件举例
#每天下午4时,5时,6时的5min,15min时执行命令df
5,15  16,17,18   *  *  * df
​
#每周二,四,五的下午五时系统进行维护状态,重新启动系统
0   17  *   *   2,4,5   shutdown -r +5
​
#每个月的1日和20日检查/dev/sdb8磁盘设备
0   0   1,20    *   *   fsck /dev/sdb8
​
#每周六的3时30分执行/webdata/bin/backup.sh
30  3   *   *   6   /webdata/bin/backup.sh

4.crontab的实践

第一步:检查crond服务开启情况:

[root@localhost ~]# systemctl status crond.service 
如果服务被关闭,则使用命令开启crond服务
[root@localhost ~]# systemctl start crond
​
第二步:创建crontab文件
crontab文件创建成功后所有文件都保存在/var/spool/cron路径下,且文件名与创建该文件时指定的用户名相同
​
第一种方法:使用命令crontab -e,然后编辑crontab文件。

[root@localhost ~]# crontab -e  #编辑crontab文件
no crontab for root - using an empty one
crontab: installing new crontab
[root@localhost ~]# cat /var/spool/cron/root //查看编辑好的crontab文件
0-59  *  *  *  * echo 111>> /tmp/a.txt  #每分钟打印111追加到/tmp/a.txt文件中
[root@localhost ~]# crontab -l  //查看crontab列表
0-59  *  *  *  * echo 111>> /tmp/a.txt
[root@localhost ~]# 
第二种方法:首先创建需导入的文件,用户为wanan,在文件中编写crontab文件内容,然后使用"crontab 文件名"将文件导入

[root@localhost ~]# su wanan  #切换到wanan用户
[wanan@localhost root]$ vim /tmp/crontab #编辑/tmp/crontab文件
[wanan@localhost root]$ cat /tmp/crontab  #查看文件内容
0-59 * * * * echo 1111 > /tmp/b.txt
[wanan@localhost root]$ cd /tmp/ #切换到tmp目录下
[wanan@localhost tmp]$ ls  #查看文件内容
a.txt  
crontab
......
[wanan@localhost tmp]$ crontab crontab 
[wanan@localhost tmp]$ crontab -l
0-59 * * * * echo 1111 > /tmp/b.txt
[wanan@localhost tmp]$ su
密码:
[root@localhost tmp]# cd /var/spool/cron/
[root@localhost cron]# ls
root  wanan
[root@localhost cron]# 
​
​
第三步:检查运行情况。可以通过查看cron日志或者查看输出文本内容检查运行情况

[root@localhost ~]# tail -f /var/log/cron  #查看cron日志
May 12 03:46:01 serverb CROND[4197]: (root) CMD (echo 111>> /tmp/a.txt)
May 12 03:46:01 serverb CROND[4205]: (wanan) CMD (echo 1111 > /tmp/b.txt)
May 12 03:47:01 serverb CROND[4265]: (root) CMD (echo 111>> /tmp/a.txt)
May 12 03:47:01 serverb CROND[4272]: (wanan) CMD (echo 1111 > /tmp/b.txt)
.....
​
[root@localhost ~]# tail  -f /tmp/b.txt  #查看b.txt文本追加情况
1111
......
​
​

扩展:使用”crontab 文件名“将文件导入时报错

[wanan@localhost tmp]$ crontab crontab  #将crontab文件导入crontab中
You (wanan) are not allowed to access to (crontab) because of pam configuration.
​
解决步骤:
1)查看/bin/crontab的SSID位是否标记S

[root@localhost tmp]# ll /bin/crontab 
-rwsr-xr-x. 1 root root 65928 5月  11 2019 /bin/crontab
结果:正常
​
2)检查/etc/cron* 是否设置了.deny文件限制;

[root@localhost tmp]# ll -dl /etc/cron*
drwxr-xr-x. 2 root root  39 12月 24 09:55 /etc/cron.d
drwxr-xr-x. 2 root root  23 12月 24 09:55 /etc/cron.daily
-rw-r--r--. 1 root root   0 5月  11 2019 /etc/cron.deny
drwxr-xr-x. 2 root root  22 12月 24 09:53 /etc/cron.hourly
drwxr-xr-x. 2 root root   6 5月  11 2019 /etc/cron.monthly
-rw-r--r--. 1 root root 451 5月  11 2019 /etc/crontab
drwxr-xr-x. 2 root root   6 5月  11 2019 /etc/cron.weekly
结果:正常
​
​
3)检查PAM模块,cat /etc/pam.d/crond,文件中配置是否正常

[root@localhost tmp]# cat /etc/pam.d/crond 
#
# The PAM configuration file for the cron daemon
#
#
# Although no PAM authentication is called, auth modules
# are used for credential setting
auth       include    password-auth
account    required   pam_access.so
account    include    password-auth
session    required   pam_loginuid.so
session    include    password-auth
结果:正常
​
4)查看系统日志cat /var/log/secure的错误信息

[root@localhost tmp]# cat /var/log/secure
​
5)查看oracle用户密码过期

[root@localhost tmp]# chage -l wanan   #发现用户密码已经过期 
最近一次密码修改时间                  :1月 17, 2022
密码过期时间                         :2月 06, 2022
密码失效时间                  :从不
帐户过期时间                      :从不
两次改变密码之间相距的最小天数     :0
两次改变密码之间相距的最大天数     :20
在密码过期之前警告的天数    :7
​
[root@localhost tmp]# chage -M 99999 wanan #设置用户密码永不过期
[root@localhost tmp]# chage -l wanan
最近一次密码修改时间                  :1月 17, 2022
密码过期时间                  :从不
密码失效时间                  :从不
帐户过期时间                      :从不
两次改变密码之间相距的最小天数     :0
两次改变密码之间相距的最大天数     :99999
在密码过期之前警告的天数    :7
[root@localhost tmp]# 
​

二:Linux后台管理

1.后台介绍

#   Linux系统可以在不关闭当前操作的情况下执行其他操作。例如:用户在当前终端正在编辑文件,在不停止编辑该文件的情况下,可以将该编辑任务暂时放入linux的后台运行,这种命令放入后台,然后将命令恢复到前台的操作一般称为后台工作管理。

2.后台命令介绍

①命令"&"放入后台
#如果向将某种执行任务在后台运行,则可以在命令后加入"&",使用这种方法放入后台的命令,在后台处于执行状态。
举例:查找install.log文件并在后台运行
[root@localhost ~]# find / -name install.log &
[3] 5346
把find命令放入后台执行,每个后台命令将被分配一个工作号。执行该命令会产生一个进程。
​
[3]   已完成               find / -name install.log
当后台命令执行完成后显示如下:
②使用[Ctrl + Z]组合键将命令暂停并放入后台
#作业空值允许将进程挂起并可以在需要时恢复进程的运行,被挂起的作业恢复后将从中止处开始继续运行。
#使用ctrl+Z组合键即可挂起当前的前台作业。
[root@localhost ~]# tail  -f /tmp/b.txt 
1111
^Z
[3]+  已停止               tail -f /tmp/b.txt   #命令放入后台,工作好是3,状态是暂停
[root@localhost ~]# jobs
[1]   已停止               tail -f /var/log/cron
[2]-  已停止               tail -f /tmp/a.txt
[3]+  已停止               tail -f /tmp/b.txt
[root@localhost ~]# 
③查找当前的工作状态(jobs)
参数说明
-l列出工作号与PID
-r仅列出正在后台运行的工作
-s仅列出正在后台挂起(暂停)的工作
[root@localhost ~]# jobs -l
[1]   4480 停止                  tail -f /var/log/cron
[2]-  4545 停止                  tail -f /tmp/a.txt
[3]+  5783 停止                  tail -f /tmp/b.txt
[root@localhost ~]# jobs -r
[root@localhost ~]# jobs -s
[1]   已停止               tail -f /var/log/cron
[2]-  已停止               tail -f /tmp/a.txt
[3]+  已停止               tail -f /tmp/b.txt
[root@localhost ~]# 
​
④后台工作恢复:fg,bg
1.fg命令
fg命令可以将后台工作恢复到前台执行
fg语法:fg  %jobnumber(工作号)

[root@localhost ~]# jobs -l
[1]   4480 停止                  tail -f /var/log/cron
[2]-  4545 停止                  tail -f /tmp/a.txt
[3]+  5783 停止                  tail -f /tmp/b.txt
[root@localhost ~]# fg %3   #取出工作号为3的后台工作
tail -f /tmp/b.txt  #可以看出已经恢复到前台
1111
​
2.bg命令
bg命令可以将后台下的任务状态可以从stopped切换成Running
bg语法:bg  %jobnumber(工作号)

[root@localhost ~]# jobs -l
[1]   4480 停止                  tail -f /var/log/cron
[2]-  4545 停止                  tail -f /tmp/a.txt
[3]+  5783 停止                  tail -f /tmp/b.txt
[root@localhost ~]# bg %3
[3]+ tail -f /tmp/b.txt &
[root@localhost ~]# jobs -r
[3]   运行中               tail -f /tmp/b.txt &
[root@localhost ~]# 

⑤后台工作关闭:kill
#kill命令可以让后台中的工作进行删除
语法:kill  [信号类型]  jobnumber(工作号)

[root@localhost ~]# jobs
[1]-  已停止               tail -f /var/log/cron
[2]+  已停止               tail -f /tmp/a.txt
[3]   运行中               tail -f /tmp/b.txt &
[root@localhost ~]# kill -9 %1   #强制杀死工作
[1]-  已杀死               tail -f /var/log/cron
[root@localhost ~]# jobs
[2]+  已停止               tail -f /tmp/a.txt
[3]-  运行中               tail -f /tmp/b.txt &
[root@localhost ~]# kill -15 %3  #正常关闭工作 
[root@localhost ~]# jobs
[2]+  已停止               tail -f /tmp/a.txt
[3]-  已终止               tail -f /tmp/b.txt
[root@localhost ~]# 
 
⑥命令后台运行脱离终端(nohup)
实现在远程终端执行后台命令的方法
#nohup命令可以让后台工作在离开操作终端时,也能正确的在后台执行
语法:nohup  [命令]  & 
举例:系统后台执行shell脚本并验证nohup功能
第一步:编写shell脚本

[root@localhost ~]# vim for.sh
[root@localhost ~]# cat for.sh 
#!/bin/bash
for ((i=0;i<100;i++))
do
    echo 11 >> /root/for.log
    sleep 10s
done
​
第二步:为for.sh赋予权限

[root@localhost ~]# chmod 755 for.sh 
​
第三步:后台执行for.sh脚本

[root@localhost ~]# nohup /root/for.sh  &
[3] 7983
​
第四步:查看ps aux查看for.sh进程

[root@localhost ~]# ps aux | grep for.sh 
root       7983  0.0  0.1  12932  3160 pts/0    S    04:51   0:00 /bin/bash /root/for.sh
root       8048  0.0  0.0  12344  1128 pts/0    S+   04:51   0:00 grep --color=auto for.sh
[root@localhost ~]# 
​
第五步:重启主机,再次使用ps aux查看进程
[root@192 ~]# ps aux | grep for.sh
root       2956  0.0  0.0  12344  1092 pts/0    S+   04:57   0:00 grep --color=auto for.sh
[root@192 ~]# 

  • 20
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值