Linux 命令之 crontab 定时计划

ugOk.gif

1、简介

crontab 命令用于设置周期性被执行的指令。该命令从标准输入设备读取指令,并将其存放于“crontab”文件中,以供之后读取和执行

cron 系统调度进程。可以使用它在每天的非高峰负荷时间段运行作业,或在一周或一月中的不同时段运行。cron是系统主要的调度进程,可以在无需人工干预的情况下运行作业

crontab 命令允许用户提交、编辑或删除相应的作业。每一个用户都可以有一个crontab文件来保存调度信息。系统管理员可以通过 cron.deny 和 cron.allow 这两个文件来禁止或允许用户拥有自己的crontab文件

2、语法参数

 crontab [ -u user ] file

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

3、参数说明

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

4、环境准备

系统安装完成后,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 -y install crontabs,如果没有安装时,可以使用yum安装

5、查看服务状态

查看服务状态,

方式一: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-2720:56:26 CST; 16min ago
 Main PID: 885(crond)
   CGroup: /system.slice/crond.service
           └─885 /usr/sbin/crond -n 7月 2720:56:26 localhost.localdomain systemd[1]: Started Command Scheduler.
 7月 2720:56:26 localhost.localdomain systemd[1]: Starting Command Scheduler...
 7月 2720:56:27 localhost.localdomain crond[885]: (CRON) INFO (RANDOM_DELAY will be scaled with factor 1% ifused.)
 7月 2720: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-2720:56:26 CST; 17min ago
 Main PID: 885(crond)
   CGroup: /system.slice/crond.service
           └─885 /usr/sbin/crond -n 7月 2720:56:26 localhost.localdomain systemd[1]: Started Command Scheduler.
 7月 2720:56:26 localhost.localdomain systemd[1]: Starting Command Scheduler...
 7月 2720:56:27 localhost.localdomain crond[885]: (CRON) INFO (RANDOM_DELAY will be scaled with factor 1% ifused.)
 7月 2720:56:27 localhost

6、开/关/重启服务

同理也有 service 与 systemctl 两种方式开启服务:

service crond start
systemctl start crond.service

重启服务:

service crond restart
systemctl restart crond.service

关闭服务:

service crond stop
systemctl stop crond.
 [root@localhost ~]# service crond stop
 Redirecting to /bin/systemctl stopcrond.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-2721:21:14 CST; 8s ago
 Process: 885ExecStart=/usr/sbin/crond -n$CRONDARGS(code=exited, status=0/SUCCESS)
 Main PID: 885(code=exited, status=0/SUCCESS)

 7月 2720:56:26 localhost.localdomain systemd[1]: Started Command Scheduler.
 7月 2720:56:26 localhost.localdomain systemd[1]: Starting Command Scheduler...
 7月 2720:56:27 localhost.localdomain crond[885]: (CRON) INFO (RANDOM_DELAY will be scaled with factor 1% ifused.)
 7月 2720:56:27 localhost.localdomain crond[885]: (CRON) INFO (running with inotify support)
 7月 2721:21:14 localhost.localdomain systemd[1]: Stopping Command Scheduler...
 7月 2721: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-2721:24:13 CST; 10s ago
 Main PID: 2861(crond)
   CGroup: /system.slice/crond.service
           └─2861 /usr/sbin/crond -n 7月 2721:24:13 localhost.localdomain systemd[1]: Started Command Scheduler.
 7月 2721:24:13 localhost.localdomain systemd[1]: Starting Command Scheduler...
 7月 2721:24:13 localhost.localdomain crond[2861]: (CRON) INFO (RANDOM_DELAY will be scaled with factor 33% ifused.)
 7月 2721:24:14 localhost.localdomain crond[2861]: (CRON) INFO (running with inotify support)
 7月 2721:24:14 localhost.localdomain crond[2861]: (CRON) INFO (@reboot jobs will be run at computer's startup.)
 [root@localhost ~]#

7、任务表格式

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

 minute   hour   day   month   week   command

8、格式说明

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 个小时整都要执行

9、常用举例

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

直接使用 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]# vim dateLog.txt 
 2019年 10月 12日 星期五 14:02:01 CST
 2019年 10月 12日 星期五 14:03:01 CST
 2019年 10月 12日 星期五 14:04:01 CST
 2019年 10月 12日 星期五 14:05:01 CST
 2019年 10月 12日 星期五 14:06:01 CST
 2019年 10月 12日 星期五 14:07:02 CST
 2019年 10月 12日 星期五 14:08:01 CST
 2019年 10月 12日 星期五 14:09:01 CST
 2019年 10月 12日 星期五 14:10:01 CST

2.每天05:00重启系统

crontab -e 编辑当前用户 crontab文件,进入编辑模式,输入 0 5 * * * /sbin/reboot

 [root@localhost sbin]# crontab -e
 */1 * * * * date >> /home/dateLog.txt 0 5 * * * /sbin/reboot

3.定时重启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

10、执行自定义脚本文件

1.准备脚本文件

在/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]#

2.定时执行

设置每隔 1 分钟执行一次 /home/test.sh 可执行文件

[root@localhost home]# lstest.sh[root@localhost home]# crontab -e*/1 * * * * /home/test.sh

定时执行的效果如下:

 [root@localhost home]# ls
 log.txt test.sh [root@localhost home]# vim log.txt 
 2019年 10月 12日 星期五 23:13:01 CST
 haha 2019年 10月 12日 星期五 23:14:01 CST
 haha 2019年 10月 12日 星期五 23:15:01 CST
 haha 2019年 10月 12日 星期五 23:16:01 CST
 haha

11、其它示例

周一到周五每天下午 5:00 寄一封信给 [email protected]

 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" -m time+4 -exec rm {} \; 每周一至周五3点钟,在目录/home中,查找文件名为*.xxx的文件,并删除4天前的文件。 30 6 */10 * * ls 意思是每月的1、11、21、31日6:30执行一次ls命

感谢您的来访,获取更多精彩文章请收藏本站。

© 版权声明
THE END
喜欢就支持一下吧
点赞1684赞赏 分享
评论 共1条
头像
请认真评论,发表纯数字纯表情做禁言处理!
提交
头像

昵称

取消
昵称表情代码图片
    • 头像雷达探币0