Search code examples
linuxcentoscroncron-task

Shell script runs manually but not executed through cron job


I am using a shell script to delete log files in my Linux machine. And I have used crontab to execute this job periodically. I can execute this script manually and see it is working fine. But the script is not triggered through cron job.

Shell script:

[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 entry: (Runs every minute)

[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. But I couldn't see any logs in the targeted log file "cronjob_log.log"

cron logs:

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?


Solution

  • I have found the solution. That I have removed the user from crontab entry & it worked.

    Original crontab entry:

    [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? Can someone explain?