繁体   English   中英

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

[英]Shell script runs manually but not executed through cron job

我正在使用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)

有人可以帮我解决这里的问题吗?

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

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

我找到了解决方案。 我已经从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作业中的用户输入不起作用? 有人可以解释吗?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM