crontab设置定时任务

crontab用于配置定时任务,主要参数是:

-e:编辑当前用户的定时任务
-r:删除当前用户的定时任务
-l:列出当前用户的定时任务

其一般格式是:

* * * * * shell_command

# 第一个*号表示分钟:范围在0-59
# 第二个*号表示小时:0-23
# 第三个*号表示一个月的第几天:1-31
# 第四个*号表示月份:1-12;或者英文缩写,例如jan等
# 第五个*号表示星期:0-6,(星期天=0或者7);或者英文缩写,例如sun,mon,fri等

# shell_command:命令行脚本命令

设置方法:crontab -e ---> i 进入编辑 ---> 设置任务 ---> Esc ---> :wq 保存退出。

例子:

# 每天凌晨1:30执行
30 1 * * * echo "test_crontab" >> /home/test_crontab.log

# 每周一到周五的中午12点执行
0 12 * * 1-5 /home/test_crontab/test.sh

# 每个月15号的2点钟执行
0 2 15 * * /home/test_crontab/test.sh

# 每天的2:30和17:30执行
30 2,17 * * * /home/test_crontab/test.sh

# 每10分钟执行
*/10 * * * * /home/test_crontab/test.sh

# 每2小时执行
0 */2 * * * /home/test_crontab/test.sh

# 在每年的6月份,每天8-12点,每隔2小时执行
0 8-12/2 * 6 * /home/test_crontab/test.sh

# 每天的8-18点,每隔2小时的15分执行
15 8-18/2 * * * /home/test_crontab/test.sh

查询定时任务执行情况

cat /var/log/cron

或者:

cat /var/spool/mail/root
# 通过mail查看,root此时表示当前用户;其他用户也有对应mail文件

如果结果文件内容太长,可以使用 more 或 less 或 tail 等查看。

 

#

https://www.runoob.com/linux/linux-comm-crontab.html

https://www.cnblogs.com/zoulongbin/p/6187238.html

https://www.jianshu.com/p/87e48ae95e44

posted on 2020-08-30 23:15  落日峡谷  阅读(3706)  评论(0编辑  收藏  举报

导航