Crontab

格式

*  *  *  *  *  command
分  时  日  月  周  命令

minute 0~59
hour 0~23
day 0~31
month 1~12
week 0~7,0或7代表星期日

1
2
3
4
5
* * * * *      每分钟
*/5 * * * * 每5分钟
0 * * * * 每小时
0 23 * * * 每天23:00
0 10,14,18,23 * * * 每天10:00, 14:00, 18:00, 23:00

创建

创建任务

1
crontab -e

查看任务

1
2
crontab -l
cat /etc/passwd | cut -f 1 -d : |xargs -I {} crontab -l -u {}

查看日志

1
cat /var/log/cron

启动

1
2
3
/sbin/service crond start
/sbin/service crond stop
/sbin/service crond restart

示例

基本

1
* * * * * date >> date.txt

执行sh脚本

1
2
3
4
5
6
7
8
*/30 * * * * /root/update-hexo.sh

#!/bin/bash

. /etc/profile
. ~/.bash_profile

hexo g

参考

Crontab in Linux with 20 Useful Examples to Schedule Jobs
Crontab error “/bin/sh: root: command not found”
Where can I set environment variables that crontab will use?