首页 文章

Shell脚本手动运行但不通过cron作业执行

提问于
浏览
0

我使用shell脚本删除Linux机器中的日志文件 . 我已经使用crontab定期执行这项工作 . 我可以手动执行此脚本,看它工作正常 . 但脚本不是通过cron作业触发的 .

Shell脚本:

[root @ testVM-dev log] #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)

有人可以帮我解决这里出了什么问题吗?

2 回答

  • 0

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

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

  • 0

    我找到了解决方案 . 我已经从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作业中的用户输入不起作用?谁能解释一下?

相关问题