*/1 * * * * /usr/bin/curl http://oa.shijizhongyun.com/api/sync/everyMinute
添加链接描述
情况一:正常情况(系统有service命令)
重启服务命令:
[root@centos6 /]
启动服务命令:
[root@centos6 /]
停止服务命令:
[root@centos6 /]
情况二:当linux发行的版本没有service这个命令时候,用如下命令进行停止启动:
停止服务:
[root@centos6 /]
启动服务:
[root@centos6 /]
1.使用yum命令安装Crontab:
yum install vixie-cron
yum install crontabs
注:vixie-cron软件包是cron的主程序;
crontabs软件包是用来安装、卸装、或列举用来驱动 cron 守护进程的表格的程序。
cron是linux的内置服务,但它不自动起来,可以用以下的方法启动、关闭这个服务:
/sbin/service crond start
/sbin/service crond stop
/sbin/service crond restart
/sbin/service crond reload
**crontab的一些基本命令
systemctl start crond.service 启动服务
systemctl stop crond.service 停止服务
systemctl restart crond.service 重启服务
systemctl reload crond.service 重载配置文件
systemctl status crond.service 查看状态
crontab -l 显示crontab文件(显示已设置的定时任务)
crontab -e 编辑crontab文件(编辑定时任务)
crontab -r 删除crontab文件(删除定时任务)
crontab -ir 删除crontab文件提醒用户(删除定时任务)**
1
2
3
4
5
6
7
8
9
2.查看Crontab状态:
service crond status
ntsysv
chkconfig –level 35 crond on
1
2
3
在这里插入图片描述
3.添加定时任务:
crontab -e
i
30 3 * * * /usr/local/etc/rc.d/lighttpd restart
ESC
:wq
service crond restart
1
2
3
4
5
6
4.查看任务列表:
crontab -l
1
5.Crontab相关命令:
(1)语 法:
crontab [-u <用户名称>][配置文件] 或 crontab { -l | -r | -e }
-u
-l
-r
-e
(2)命令时间格式 :
* * * * * command
分 时 日 月 周 命令
第1列表示分钟1~59 每分钟用*或者 */1表示
第2列表示小时1~23(0表示0点)
第3列表示日期1~31
第4列表示月份1~12
第5列标识号星期0~6(0表示星期天)
第6列要运行的命令
(3)一些Crontab定时任务例子:
30 21 * * * /usr/local/etc/rc.d/lighttpd restart
45 4 1,10,22 * * /usr/local/etc/rc.d/lighttpd restart
10 1 * * 6,0 /usr/local/etc/rc.d/lighttpd restart
0,30 18-23 * * * /usr/local/etc/rc.d/lighttpd restart
0 23 * * 6 /usr/local/etc/rc.d/lighttpd restart
* 23-7/1 * * * /usr/local/etc/rc.d/lighttpd restart
* */1 * * * /usr/local/etc/rc.d/lighttpd restart
0 11 4 * mon-wed /usr/local/etc/rc.d/lighttpd restart
0 4 1 jan * /usr/local/etc/rc.d/lighttpd restart
*/30 * * * * /usr/sbin/ntpdate cn.pool.ntp.org
0 */2 * * * /sbin/service httpd restart
50 7 * * * /sbin/service sshd start
50 22 * * * /sbin/service sshd stop
0 0 1,15 * * fsck /home
1 * * * * /home/bruce/backup
00 03 * * 1-5 find /home "*.xxx" -mtime +4 -exec rm {} \;
30 6 */10 * * ls
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
6.部分脚本无法执行问题:
如果我们使用 crontab 来定时执行脚本,无法执行,但是如果直接通过命令(如:./test.sh)又可以正常执行,这主要是因为无法读取环境变量的原因。
解决方法:
(1)所有命令需要写成绝对路径形式,如: /usr/local/bin/docker。
(2)在 shell 脚本开头使用以下代码:
. /etc/profile
. ~/.bash_profile
1
2
3
4
5
(3)在 /etc/crontab 中添加环境变量,在可执行命令之前添加命令 . /etc/profile;/bin/sh,使得环境变量生效,例如:
20 03 * * * . /etc/profile;/bin/sh /var/www/wwwroot/test.sh
1
7.Crontab默认调度任务:
cron默认配置了调度任务,分别为:hourly、daily、weekly、mouthly,默认配置文件为/etc/anacrontab
将需要执行的脚本放到相应的目录下即可,目录分别为:
/etc/cron.hourly
/etc/cron.daily
/etc/cron.weekly
/ect/cron.mouthly
1
2
3
4
8.注意清理系统用户的邮件日志:
每条任务调度执行完毕,系统都会将任务输出信息通过电子邮件的形式发送给当前系统用户,这样日积月累,日志信息会非常大,可能会影响系统的正常运行,因此,将每条任务进行重定向处理非常重要。
例如,可以在crontab文件中设置如下形式,忽略日志输出:
0 */3 * * * /usr/local/apache2/apachectl restart >/dev/null 2>&1
1
“/dev/null 2>&1”表示先将标准输出重定向到/dev/null,然后将标准错误重定向到标准输出,由于标准输出已经重定向到了/dev/null,因此标准错误也会重定向到/dev/null,这样日志输出问题就解决了。
9.Crontab日志路径:
ll /var/log/cron*
1
在这里插入图片描述
10.Crontab命令在线验证工具:
https://tool.lu/crontab