I have a cron job supposed to trigger a shell script daily at 2 AM.

0 2 * * * /root/bin/script.sh

However it does not work at all. What am I missing?

More details: The script runs fine without cron scheduling when run manually and does what it is supposed to do. Root user is running the cron job. The cron job was scheduled by crontab -e as root user. pgrep cron gives a service id which means that cron service is running. Following are the contents of /root/bin/script.sh file:

exec 1> >(while IFS= read -r line; do echo "$(date --rfc-3339 ns) $line"; done | tee -a ${BACKUP_LOG}) 2>&1

# Back up the etc directory
mkdir /home/directory1/backup/etc_backup
cp -Lrp /etc /home/data/backup/etc_backup
tar czf /home/data/backup/etc_backup.tgz
rm -rf /home/data/backup/etc_backup

Actually I moved the script from /etc/cron.daily/ to /root/bin. Is the script supposed to be in /etc/cron.daily only for daily execution?

  • 2
    Please edit your question and give us more details. What user is running this crontab? How did you add it? Did you run crontab -e (as what user?) or did you edit /etc/cron/crontab? Does the script work if run manually? What does the script do? Does it call other programs? We need to see the script to know if it would work with cron. Is your cron service running? Give us something to go on, we don't have access to your machine! – terdon Nov 4 '16 at 12:54
  • @terdon I have added the details in the post. – hasnain rehman Nov 4 '16 at 13:24

My guess is that your script isn't being understood properly by the shell, because it doesn't have any proper shebang. Try using crontab -e with this instead:

0 2 * * * bash /root/bin/script.sh > /tmp/crontest.log 2>&1

By invoking the script directly using bash, the script should run fine now. Any output should end up logged to /tmp/crontest.log as well, which could help a bit with debugging if it still doesn't work.

  • I have tried running 57 14 * * * bash /root/bin/script.sh > /tmp/crontest.log 2>&1 in the crontab. ` /tmp/crontest.log` does not show any output and the script is still not working. – hasnain rehman Nov 4 '16 at 13:57
  • Sorry, previously I did not do the test correctly. Your guess was correct, the script did not have the proper shebang. The is resolved by adding: #!/usr/bin/env bash in the beginning of the script. – hasnain rehman Nov 4 '16 at 15:21
  • 1
    @hasnain rehman if this has answered your question, please mark this as the answer. – steve Nov 4 '16 at 16:00
  • 1
    @steve done. Iskar thanks for the answer. – hasnain rehman Nov 4 '16 at 16:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.