1

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:

BACKUP_LOG=/var/log/backup.log
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
/home/data/backup/etc_backup
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
  • 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, 2016 at 12:54
  • @terdon I have added the details in the post. Nov 4, 2016 at 13:24

1 Answer 1

3

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.

4
  • 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. Nov 4, 2016 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. Nov 4, 2016 at 15:21
  • 1
    @hasnain rehman if this has answered your question, please mark this as the answer.
    – steve
    Nov 4, 2016 at 16:00
  • 1
    @steve done. Iskar thanks for the answer. Nov 4, 2016 at 16:49

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .