Crontab不生效等常见问题解决方法参考:https://www.jb51.net/article/154290.htm
比如:有些时候需要将指定python版本加入到PATH环境变量中,因为某些模块安装到了指定的Python的版本上,而非系统默认的Python。
--------------------------------------------------------------------------------------------------------------------------------------------
1. 帮助命令
$ crontab -h
crontab: invalid option -- 'h'
crontab: usage error: unrecognized option
usage: crontab [-u user] file
crontab [-u user] [ -e | -l | -r ]
(default operation is replace, per 1003.2)
-e (edit user's crontab)
-l (list user's crontab)
-r (delete user's crontab)
-i (prompt before deleting user's crontab)
-s (selinux context)
2.列出当前用户配置的Crontab任务
$ crontab -l
* * * * * /bin/sh /home/work/rongsong/crontab/test.sh
test.sh脚本内容如下,其中test.log假设为定期会生成的日志文件,我们的目的就是定期删除它,注意这里文件必须写成绝对路径,否则可能会不生效。
rm -rf /home/work/rongsong/crontab/test.log
如果写成下面这种方式的话,定时任务会不生效,尽管test.log文件存在于目录/home/work/rongsong/crontab下。
cd /home/work/rongsong/crontab
rm -rf test.log
补充:
如何查看crontab定时任务是否执行(这个方法我的机器上没有成功,因此采用的是第二种方式)
(1)查看crontab的日志
日志文件为/var/log/cron
找到对应时间,是否执行指令
这种方式只能看到是否执行,但是并无法确定是否执行成功。
(2)将定时任务的日志重定向
日志重定向的时候要注意,要将标准错误日志一起重定向,才能获取到正常和错误的日志,例如:
* * * * * /bin/sh -x /home/work/rongsong/crontab/test.sh >> /home/work/rongsong/crontab/my.log 2>&1
3.编辑修改当前用户配置的Crontab任务
$ crontab -e
注意:删除指定的定时任务时,一定要谨慎,不熟悉的同学不要轻易使用crontab -r 命令,直接使用crontab -e编辑删除即可。