Solve Crontab Job Error.Configuration,Example Script,expression,Solve Problem Linux
Tuesday, December 17
Cron fails with exit status 1
Been fighting this one for over one and a half hour now.. can't figure it out.
Cron doesn't execute it's commands. To test if it was my shell script I even added a really simple cron (make a directory), that also doesn't work.
crontab -l reports:
* * * * * /var/bashes/backup_codeblocks.sh 2>&1 >> /var/bashes/logs/backup_codeblocks.log
* * * * * sudo mkdir /var/testard
backup_codeblocks.sh:
Code:
#!/bin/bash
cd /home/henk/Documents/
date=`date +%Y-%m-%d_%R`
sudo tar cvpzf /var/backups-manual/Codeblocks_$date.tgz --exclude=*/bin Codeblocks
/var/bashes folder is chmodded 777
/var/backups-manual is chmodded 777
backup_codeblocks.sh is chmodded u+x and works fine when run manually.
The cron does create the backup_codeblocks.log file but it's empty.
/var/log/syslog:
Code:
Mar 3 14:40:01 henk-HP-G62-Notebook-PC CRON[7277]: (henk) CMD (/var/bashes/backup_codeblocks.sh 2>&1 >> /var/bashes/logs/backup_codeblocks.log)
Mar 3 14:40:01 henk-HP-G62-Notebook-PC CRON[7278]: (henk) CMD (sudo mkdir /var/testard)
Mar 3 14:40:01 henk-HP-G62-Notebook-PC CRON[7275]: (CRON) error (grandchild #7278 failed with exit status 1)
Mar 3 14:40:01 henk-HP-G62-Notebook-PC CRON[7275]: (CRON) info (No MTA installed, discarding output)
Mar 3 14:40:01 henk-HP-G62-Notebook-PC CRON[7276]: (CRON) error (grandchild #7277 failed with exit status 1)
Mar 3 14:40:01 henk-HP-G62-Notebook-PC CRON[7276]: (CRON) info (No MTA installed, discarding output)
What gives?
Solution:
Often cron failures are due to environment issues. For security the environment that cron provides is not the same as the user's environment. There is an easy fix that also happens to be a best practice for all scripting.
Always provide the full path to any external commands inside your scripts.
'tar' should be /bin/tar (at least on my system) and 'date' is /bin/date. Your specific locations may be different.
Lastly, you didn't actually verify that that the 'cd' command worked. You could be in any directory. What happens if that directory doesn't exist?
With bash scripting, you may want to enable stop on error, noclobber, and catch any signals.
Ok, with all that said, I looked at your errors and cron wants to send an email out when it finishes. That means you need an MTA like postfix installed. That is the MTA error.
Thanks for your tips. I figured out how to fix it though. Rather than editing my own users crontab (crontab -e) I should have been editing sudo crontab -e. It works now
0 comments:
Post a Comment