[英]Shell script runs manually but not executed through cron job
I am using a shell script to delete log files in my Linux machine. 我正在使用Shell脚本删除Linux计算机中的日志文件。 And I have used crontab to execute this job periodically. 而且我已经使用crontab定期执行此作业。 I can execute this script manually and see it is working fine. 我可以手动执行此脚本,并可以正常运行。 But the script is not triggered through cron job. 但是脚本不是通过cron作业触发的。
Shell script: Shell脚本:
[root@testVM-dev log]# vim /path_to_dir/log_cleanup.sh [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 entry: (Runs every minute) crontab条目:(每分钟运行一次)
[root@testVM-dev log]# crontab -l
*/1 * * * * root /path_to_dir/log_cleanup.sh
Also I have checked in cron logs that the job is executed every minute. 另外,我已经在cron日志中检查了该作业每分钟执行一次。 But I couldn't see any logs in the targeted log file "cronjob_log.log" 但是我在目标日志文件“ cronjob_log.log”中看不到任何日志
cron logs: cron日志:
vim /var/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)
Can someone help me on what is going wrong here? 有人可以帮我解决这里的问题吗?
Besides manually specifying value of PATH
, try specifying the path to tee
and date
. 除了手动指定PATH
值外,请尝试指定tee
和date
的路径。
/usr/bin/date
, /usr/bin/tee
/usr/bin/date
, /usr/bin/tee
I have found the solution. 我找到了解决方案。 That I have removed the user from crontab entry & it worked. 我已经从crontab条目中删除了用户,并且它起作用了。
Original crontab entry: 原始crontab条目:
[root@testVM-dev log]# crontab -l
*/1 * * * * root /path_to_dir/log_cleanup.sh
After modification: 修改后:
[root@testVM-dev log]# crontab -l
*/1 * * * * /path_to_dir/log_cleanup.sh
Now I got confused why the user entry in cron job didn't work? 现在我很困惑,为什么cron作业中的用户输入不起作用? Can someone explain? 有人可以解释吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.