【问题标题】:Shell script runs manually but not executed through cron jobShell 脚本手动运行但不通过 cron 作业执行
【发布时间】: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


【解决方案1】:

除了手动指定PATH 的值外,请尝试指定teedate 的路径。

/usr/bin/date, /usr/bin/tee

【讨论】:

  • 让我检查一下。谢谢.. :)
  • 它没有用。我删除了日期并给出了 /usr/bin/tee 的完整路径。但我仍然面临同样的问题..
【解决方案2】:

我找到了解决方案。我已经从 crontab 条目中删除了用户并且它有效。

原始 crontab 条目:

[root@testVM-dev log]# crontab -l
*/1 * * * * root /path_to_dir/log_cleanup.sh

修改后:

[root@testVM-dev log]# crontab -l
*/1 * * * * /path_to_dir/log_cleanup.sh

现在我很困惑为什么 cron 作业中的用户条目不起作用?谁能解释一下?

【讨论】:

    最近更新 更多