Linux——Crontab定时器

Crontab定时的使用

定时:定时器最重要的在于定时上,让我们看看Crontab是如何设置定时规则的。

[root@VM-24-14-centos cron]# cat /etc/crontab 
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root

```bash
# 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
查看crontab文件内容可以看到,1个*代表week,2个*代表month
如图所示不难理解,那么我们知道了规则怎么应用到内容当中呢?

Crontab应用:

1.我们先编写一个shell文件,内容就是echo一个123出来

[root@VM-24-14-centos cron]# cat test.sh
#!/bin/bash
echo 123

[root@VM-24-14-centos cron]# 

2.命令:crontab -e命令可以查看并编辑crontab文件内容的定时任务

*/5 * * * * flock -xn /tmp/stargate.lock -c '/usr/local/qcloud/stargate/admin/start.sh > /dev/null 2>&1 &'
* * * * * sh /var/spool/cron/test.sh >> /www/test.log 2>&1                                               

第二行就是我们的定时任务,5个*就代表每分钟执行一次(可以查看定时规则进行指定)


版权声明:本文为weixin_45183997原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。