Linux 命令之 crontab 定时计划

本文导读

  • Linux crontab 是用来定期执行程序的命令。当安装完成操作系统之后,默认便会启动此任务调度命令。
  • crond 命令每分锺会定期检查是否有要执行的工作,如果有要执行的工作便会自动执行该工作。
  • linux 任务调度的工作主要分为以下两类:
  1. 系统执行的工作:系统周期性所要执行的工作,如备份系统数据、清理缓存等
  2. 个人执行的工作:某个用户定期要做的工作,例如每隔10分钟检查邮件服务器是否有新信,这些工作可由每个用户自行设置
  • crontab 是用来让使用者在固定时间或固定间隔执行程序之用,类似Windows 的定时计划

语法 参数 

语 法

crontab [ -u user ] file
或
crontab [ -u user ] { -l | -r | -e }

参数说明

  • -u user:用来设定指定用户的 crontab服务
  • -e : 编辑某个用户的crontab文件内容,没指定用户时表示编辑当前用户的crontab文件
  • -r : 删除某个用户的crontab文件,没指定用户时表示删除当前用户的crontab文件
  • -l :显示某个用户的crontab文件内容,没指定用户时表示显示当前用户的crontab文件内容

环境准备

安 装

  • 系统安装完成后,crontab 命令一般也是默认安装好了的,而且每次系统启动之后,crond 服务也是默认开启的
  • 模糊查找:yum list |grep crontab ,当不知道 crontab命令包全名时,可以如此搜索
  • 查看安装状态:yum list crontabs ,如下可知 crontab命令包全名为 “crontabs”,而且已经安装好了
[root@localhost ~]# yum list |grep crontab
crontabs.noarch                             1.11-6.20121102git.el7     @anaconda
[root@localhost ~]# yum list crontabs
已加载插件:fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.aliyun.com
 * extras: mirrors.aliyun.com
 * updates: mirrors.tuna.tsinghua.edu.cn
已安装的软件包
crontabs.noarch                                                     1.11-6.20121102git.el7                                                      @anaconda
[root@localhost ~]# 
  • 安装:yum install crontabs,如果没有安装时,可以使用yum安装

查看服务状态

  • 查看服务状态,方式一:service crond status
  • 查看服务状态,方式二:systemctl status crond.service
  • 服务名称为 crond,systemctl命令要在 CentOS7及以后版本才有,,推荐方式是 systemctl
[root@localhost ~]# service crond status
Redirecting to /bin/systemctl status  crond.service
● crond.service - Command Scheduler
   Loaded: loaded (/usr/lib/systemd/system/crond.service; enabled; vendor preset: enabled)
   Active: active (running) since 五 2018-07-27 20:56:26 CST; 16min ago
 Main PID: 885 (crond)
   CGroup: /system.slice/crond.service
           └─885 /usr/sbin/crond -n

7月 27 20:56:26 localhost.localdomain systemd[1]: Started Command Scheduler.
7月 27 20:56:26 localhost.localdomain systemd[1]: Starting Command Scheduler...
7月 27 20:56:27 localhost.localdomain crond[885]: (CRON) INFO (RANDOM_DELAY will be scaled with factor 1% if used.)
7月 27 20:56:27 localhost.localdomain crond[885]: (CRON) INFO (running with inotify support)
[root@localhost ~]# systemctl status crond.service
● crond.service - Command Scheduler
   Loaded: loaded (/usr/lib/systemd/system/crond.service; enabled; vendor preset: enabled)
   Active: active (running) since 五 2018-07-27 20:56:26 CST; 17min ago
 Main PID: 885 (crond)
   CGroup: /system.slice/crond.service
           └─885 /usr/sbin/crond -n

7月 27 20:56:26 localhost.localdomain systemd[1]: Started Command Scheduler.
7月 27 20:56:26 localhost.localdomain systemd[1]: Starting Command Scheduler...
7月 27 20:56:27 localhost.localdomain crond[885]: (CRON) INFO (RANDOM_DELAY will be scaled with factor 1% if used.)
7月 27 20:56:27 localhost

开/关/重启服务

  • 同理也有 service 与 systemctl 两种方式
  • 开启服务:service crond start ,systemctl start crond.service
  • 重启服务:service crond restart , systemctl restart crond.service
  • 关闭服务:service crond stop , systemctl stop crond.service
[root@localhost ~]# service crond stop
Redirecting to /bin/systemctl stop  crond.service
[root@localhost ~]# service crond status
Redirecting to /bin/systemctl status  crond.service
● crond.service - Command Scheduler
   Loaded: loaded (/usr/lib/systemd/system/crond.service; enabled; vendor preset: enabled)
   Active: inactive (dead) since 五 2018-07-27 21:21:14 CST; 8s ago
  Process: 885 ExecStart=/usr/sbin/crond -n $CRONDARGS (code=exited, status=0/SUCCESS)
 Main PID: 885 (code=exited, status=0/SUCCESS)

7月 27 20:56:26 localhost.localdomain systemd[1]: Started Command Scheduler.
7月 27 20:56:26 localhost.localdomain systemd[1]: Starting Command Scheduler...
7月 27 20:56:27 localhost.localdomain crond[885]: (CRON) INFO (RANDOM_DELAY will be scaled with factor 1% if used.)
7月 27 20:56:27 localhost.localdomain crond[885]: (CRON) INFO (running with inotify support)
7月 27 21:21:14 localhost.localdomain systemd[1]: Stopping Command Scheduler...
7月 27 21:21:14 localhost.localdomain systemd[1]: Stopped Command Scheduler.
[root@localhost ~]# 
[root@localhost ~]# systemctl start crond.service
[root@localhost ~]# systemctl status crond.service
● crond.service - Command Scheduler
   Loaded: loaded (/usr/lib/systemd/system/crond.service; enabled; vendor preset: enabled)
   Active: active (running) since 五 2018-07-27 21:24:13 CST; 10s ago
 Main PID: 2861 (crond)
   CGroup: /system.slice/crond.service
           └─2861 /usr/sbin/crond -n

7月 27 21:24:13 localhost.localdomain systemd[1]: Started Command Scheduler.
7月 27 21:24:13 localhost.localdomain systemd[1]: Starting Command Scheduler...
7月 27 21:24:13 localhost.localdomain crond[2861]: (CRON) INFO (RANDOM_DELAY will be scaled with factor 33% if used.)
7月 27 21:24:14 localhost.localdomain crond[2861]: (CRON) INFO (running with inotify support)
7月 27 21:24:14 localhost.localdomain crond[2861]: (CRON) INFO (@reboot jobs will be run at computer's startup.)
[root@localhost ~]# 

任务表格式

  • 用户创建的 crontab文定时件中,每一行都代表一项任务,每行的每个字段代表一项设置,它的格式共分为6个字段,前5个是时间字段,第6个是要执行的命令,格式如下:
minute    hour    day    month    week    command

格式说明

  • minute:表示分钟,取值 [0,59] 之间的任何整数
  • hour:表示小时,取值 [0,23] 之间的任何整数
  • day:表示日期, 取值 [1,31] 之间的任何整数
  • month:表示月份,取值 [1,12] 之间的任何整数
  • week:表示星期几,取值 [0,7] 之间的任何整数,0和7都代表星期日
  • command:要执行的命令,可以是系统命令,也可以是用户编写的脚本文件,如*.sh
  • 当 minute 为 “ * ” 时表示每分钟都要执行 command,hour 为 “ * ” 时表示每小时都要执行程序,其于类推
  • 当 minute 为 " a-b " 时表示从第 a 分钟到第 b 分钟这段时间内每分钟整都要执行 command,hour 为 “ a-b ” 时表示从第 a 到第 b 小时内的没小时整都要执行,其于类推
  • 当 minute 为 “ */n ” 时表示每 n 分钟个时间间隔执行一次,hour 为 " */n " 表示每 n 个小时时间间隔执行一次,其于类推
  • 当 minute 为 “ a, b, c ” 时表示第 a, b, c 分钟都要执行,hour 为 a, b, c 时表示第 a, b, c 个小时整都要执行,其于类推

常用举例

每分钟向文本写入当前日期

  • 直接使用 crontab -e  编辑当前用户的 crontab 定时计划文件内容即可
  • 创建任务之后,使用 crontab -l 可以查看当前用户的crontab文件内容
  • crontab 文件修改之后立即生效,无需重启 crond.service 服务
[root@localhost /]# crontab -e
*/1 * * * * date >> /home/dateLog.txt
[root@localhost home]# crontab -l
*/1 * * * * date >> /home/dateLog.txt
  • 结果就是每分钟都会将系统当前时间输出到 /home/dateLog.txt 文件中去,/home/dateLog.txt文件不存在时,它默认会新建
[root@localhost home]# ls
dateLog.txt
[root@localhost home]# 
[root@localhost home]# vim dateLog.txt 
2018年 07月 27日 星期五 14:02:01 CST
2018年 07月 27日 星期五 14:03:01 CST
2018年 07月 27日 星期五 14:04:01 CST
2018年 07月 27日 星期五 14:05:01 CST
2018年 07月 27日 星期五 14:06:01 CST
2018年 07月 27日 星期五 14:07:02 CST
2018年 07月 27日 星期五 14:08:01 CST
2018年 07月 27日 星期五 14:09:01 CST
2018年 07月 27日 星期五 14:10:01 CST

每天05:00重启系统

  • crontab -e 编辑当前用户 crontab文件,进入编辑模式,输入 0 5 * * * /sbin/reboot :
[root@localhost sbin]# crontab -e
*/1 * * * * date >> /home/dateLog.txt
0 5 * * * /sbin/reboot

定时重启Tomcat服务器

  • 每天凌晨6点整关闭Tomcat服务器,6点零5分再启动服务,后面的路径请以实际安装的为准
[root@localhost bin]# crontab -e
*/1 * * * * date >> /home/dateLog.txt
0 5 * * * /sbin/reboot
0 6 * * * /usr/local/tomcat_1.8/apache-tomcat-8_1/bin/shutdown.sh
5 6 * * * /usr/local/tomcat_1.8/apache-tomcat-8_1/bin/startup.sh

执行自定义脚本文件

准备脚本文件

  • 在/home目录下新建一个 “test.sh”脚本文件,linux下的 *.sh文件类似于Windows下的bat文件
  • test.sh 可执行文件的内容很简单,就是往当前目录下的log.txt输入系统日期内容以及"haha"字符串
  • 注意:很多时候*.sh脚本文件可以直接运行,但是通过crontab却不能运行,原因是因为crontab执行脚本的时候不会读取用户的环境变量等配置 ,解决办法是在 *.sh 文件的起始位置加上 "source /etc/profile" 即可
[root@localhost home]# ls
[root@localhost home]# vim test.sh
source /etc/profile
date >> /home/log.txt
echo 'haha' >> /home/log.txt
          
  • 保存然后退出
[root@localhost home]# ls
[root@localhost home]# vim test.sh
[root@localhost home]# ls
test.sh
[root@localhost home]# 
  • 默认情况下,现在这个 test.sh 文件是没有权限的,需要为它赋予权限才能使用,chmod 赋予权限之后,test.sh文件会变成绿色,即表示可执行文件
[root@localhost home]# ./test.sh
-bash: ./test.sh: 权限不够
[root@localhost home]# chmod 777 ./test.sh 
[root@localhost home]# ls
test.sh
[root@localhost home]# 

定时执行

  • 设置每隔 1 分钟执行一次 /home/test.sh 可执行文件
[root@localhost home]# ls
test.sh
[root@localhost home]# crontab -e
*/1 * * * * /home/test.sh
  • 定时执行的效果如下:
[root@localhost home]# ls
log.txt  test.sh
[root@localhost home]# vim log.txt 
2018年 07月 27日 星期五 23:13:01 CST
haha
2018年 07月 27日 星期五 23:14:01 CST
haha
2018年 07月 27日 星期五 23:15:01 CST
haha
2018年 07月 27日 星期五 23:16:01 CST
haha
~                                                                                                                                                        
~           

其它示例

0 17 * * 1-5 mail -s "hi" [email protected] < /tmp/maildata
  • 每月每天的午夜 0 点 20 分, 2 点 20 分, 4 点 20 分....执行 echo "haha"
20 0-23/2 * * * echo "haha"
0 */2 * * * /sbin/service httpd restart  意思是每两个小时重启一次apache 

50 7 * * * /sbin/service sshd start  意思是每天7:50开启ssh服务 

50 22 * * * /sbin/service sshd stop  意思是每天22:50关闭ssh服务 

0 0 1,15 * * fsck /home  每月1号和15号检查/home 磁盘 

1 * * * * /home/bruce/backup  每小时的第一分执行 /home/bruce/backup这个文件 

00 03 * * 1-5 find /home "*.xxx" -mtime +4 -exec rm {} \;  每周一至周五3点钟,在目录/home中,查找文件名为*.xxx的文件,并删除4天前的文件。

30 6 */10 * * ls  意思是每月的1、11、21、31日是的6:30执行一次ls命令

猜你喜欢

转载自blog.csdn.net/wangmx1993328/article/details/81235245