Linux实现定时任务(CentOS)

1、编辑crontab文件

配置文件路径:/etc/crontab
在配置文件中,原本6个段的配置,我们需要在命令前面再加一个用户段
即:分 时 日 月 周 用户 命令
直接添加到最后一行即可

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 * * * * root sh /data/3Ddata/3Ddata2.sh >> /data/3Ddata/logs/lwy-crontab.log &

2、重启cron(每次改动crontab文件后,都需重启crond服务)

# 重启服务
systemctl restart crond
# 查看服务状态
systemctl status  crond

3、检查定时任务执行情况

cd /var/log
tail -f cron

4、定时任务不能执行的原因

参考:http://t.zoukankan.com/MarkGrid-p-3139454.html

https://blog.csdn.net/firstcode666/article/details/122244081

使用 .bash_profile文件解决crontab定时任务不执行问题:

https://blog.csdn.net/qq_42683219/article/details/98174501

其他参考:https://www.cnblogs.com/mtima/archive/2013/03/05/2944457.html

posted @ 2023-01-10 21:02  Caesar_the_great  阅读(379)  评论(0编辑  收藏  举报