【发布时间】:2014-09-06 23:23:35
【问题描述】:
我正在使用 shell 脚本来删除我的 Linux 机器中的日志文件。我已经使用 crontab 定期执行这项工作。我可以手动执行这个脚本,看看它工作正常。但是脚本不是通过cron作业触发的。
Shell 脚本:
[root@testVM-dev 日志]# vim /path_to_dir/log_cleanup.sh
#!/bin/bash
now=$(date)
echo "Cron Job to delete log files start...@ $now" | tee -a /path_to_dir/log/cronjob_log.log
/usr/bin/find /path_to_dir/log/localhost_access_log.* -type f -mtime +5 -print -delete | tee -a /path_to_dir/log/cronjob_log.log
echo "Delete files completed!" | tee -a /path_to_dir/log/cronjob_log.log
crontab 条目:(每分钟运行一次)
[root@testVM-dev log]# crontab -l
*/1 * * * * root /path_to_dir/log_cleanup.sh
我还在 cron 日志中检查了该作业每分钟执行一次。但我在目标日志文件“cronjob_log.log”中看不到任何日志
cron 日志:
vim /var/log/cron
Jul 16 03:56:01 rstestVM-dev crond[19131]: (root) CMD (root /path_to_dir/log_cleanup.sh)
Jul 16 03:57:01 rstestVM-dev crond[19150]: (root) CMD (root /path_to_dir/log_cleanup.sh)
Jul 16 03:58:01 rstestVM-dev crond[19168]: (root) CMD (root /path_to_dir/log_cleanup.sh)
Jul 16 03:59:01 rstestVM-dev crond[19188]: (root) CMD (root /path_to_dir/log_cleanup.sh)
有人可以帮我解决这里出了什么问题吗?
【问题讨论】:
-
你能把时间间隔增加一点看看到底发生了什么
-
你的意思是把频率从 1 分钟增加到更多(比如 5 分钟左右)?
-
是的,试试加到5分钟再看
-
将频率更改为 5 分钟,但它的行为仍然相同。 */5 * * * * root /path_to_dir/log_cleanup.sh ... .log.... Jul 16 04:45:01 rstestVM-dev crond[20423]: (root) CMD (root /path_to_dir/log_cleanup.sh ) Jul 16 04:50:01 rstestVM-dev crond[20527]: (root) CMD (root /path_to_dir/log_cleanup.sh)
-
你能不能试试把 now=$(date) 改成 now=$date 看看能不能用
标签: linux cron centos crontab cron-task