天天看点

linux centos crontab定时器

这里的定时器是centos7.2自带,并且应该是设置了开机自动启动,本人进服务器就直接输入定时器状态查询语句,结果显示已经启动。

[root@**** ~]# 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
           

查看定时器状态:service crond status || systemctl status crond.service (两种方式都可以)

[root@**** ~]# systemctl status crond.service
● crond.service - Command Scheduler
   Loaded: loaded (/usr/lib/systemd/system/crond.service; enabled; vendor preset: enabled)
   Active: active (running) since Tue 2018-12-11 00:03:53 CST; 2 months 27 days ago
 Main PID: 470 (crond)
   CGroup: /system.slice/crond.service
           └─470 /usr/sbin/crond -n

Dec 11 00:03:53 **** systemd[1]: Started Com...
Dec 11 00:03:53 **** systemd[1]: Starting Co...
Dec 11 00:03:53 **** crond[470]: (CRON) INFO...
Dec 11 00:03:53 **** crond[470]: (CRON) INFO...
Hint: Some lines were ellipsized, use -l to show in full.
[root@**** ~]# 
           

启动定时器:service crond start || systemctl start crond.service

重启定时器:service crond restart || systemctl restart crond.service

关闭定时器:service crond stop || systemctl stop crond.service

重新加载:service crond reload || systemctl reload crond.service

正题:定时器操作

先创建一个文件,测试定时器是否创建成功使用

[root@**** ~]# vim /home/www/test.txt
           

输入内容 text  保存退出

查看定时器:crontab -l(当前用户)|| crontab -l -u root (指定用户);下面内容表示root用户没有创建定时器

[root@**** ~]# crontab -l
no crontab for root
[root@**** ~]# 
           

创建定时器:crontab -e (运行此命令后是直接编辑内容,同vim)

编辑完任务后建议重载一遍定时器服务,防止定时任务不生效(service crond reload || systemctl reload crond.service)

输入以下内容保存并退出(内容解释:每 2 分钟向 /home/www/test.txt 文件中追加 testCrontab内容)

*/2 * * * * echo testCrontab >> /home/www/test.txt
           
[root@**** ~]# crontab -e
no crontab for root - using an empty one
crontab: installing new crontab
[root@**** ~]# 
           

等几分钟后查看内容是否追加成功(向test.txt文件追加了4次内容,即离创建定时器已有8分钟之久,并表示定时器创建成功)

[root@**** ~]# cat /home/www/test.txt 
test
testCrontab
testCrontab
testCrontab
testCrontab
[root@**** ~]# 
           

再次查看定时器:

[root@**** ~]# crontab -l
*/2 * * * * echo testCrontab >> /home/www/test.txt
[root@**** ~]# 
           

删除定时器:crontab  -r

[root@**** ~]# crontab -r
[root@**** ~]# 
           

再次查看定时器:

[root@**** ~]# crontab -l
no crontab for root
[root@**** ~]# 
           

定时器内容解析:* * * * * command(分 时 日 月 周 命令)

  • 分:每小时的第几分钟(0-59)
  • 时:每天的几时(0-23)
  • 日:每月的第几天(1-31)
  • 月:每年的第几月(1-12)
  • 周:每周的第几天(0-6)周日为0
  • 命令:你要执行的内容

tips:

(*)代表任何时间;

(,)代表一个不连续的时间;

(-)代表连续的时间范围

(*/特定的值)代表每隔一段时间执行

举例:

*/2 * * * * echo testCrontab >> /home/www/test.txt (每2分钟向test.txt文件追加testCrontab内容)

30 7 * * 1-5 command(每周1至周5的早上7时30分执行)

*/15 23 31 12 * command(每年的12月31号晚上23时开始执行,间隔15分钟执行一次,共执行4次)

15,45 * * * * command(每小时的15分,45分各执行一次)

附加:

方案一:将定时任务内容追加在  /etc/crontab 文件后面,保存退出后,重载/etc/crontab文件(crontab /etc/crontab)

[root@**** ~]# vim /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

# 定时任务
*/1 * * * * echo testCrontab >> /home/www/test.txt
           
[root@**** ~]# crontab /etc/crontab 
           

方案二:运行脚本

创建脚本文件并给文件执行权限,然后执行方案一步骤

[root@**** ~]# vim /home/www/test.sh 
           
#!/bin/bash
# chkconfig: 2345 10 90
# description: test

echo testCrontab >> /home/www/test.txt
           
[root@**** ~]# chmod +x /home/www/test.sh 
[root@**** ~]# vim /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

# 定时任务
*/1 * * * * /home/www/test.sh
           
[root@**** ~]# crontab /etc/crontab 
           

结束

继续阅读