0

Can anyone help me please. I'm struggling with a issue since hours and can't seem to fix it.

When I execute this script via shell it works and I also get the right email output sent to me, but when it executes via cronjob it somehow always says the backup failed, which isn't correct and it worked manually executing it.

#dokuwiki related
dokudate1=$(date +"%d.%m.%y - %H.%M")
host=$(hostname)
dokudate=$(date +"%m-%d")
dokufunc=$(find|grep "dokuwiki"|grep "$dokudate" > doku.txt)

#tw related
twdate1=$(date +"%d.%m.%y - %H.%M")
host=$(hostname)
twdate=$(date +"%d.%m")
twfunc=$(find|grep "torzon"|grep "$twdate" > tw.txt)

fc1(){
$dokufunc
if
grep -R "dokuwiki" doku.txt > /dev/null
then
echo "..."
echo "dokuwiki backup successfull"
echo -en "dokuwiki backup success!" | mail -s "dokuwiki backup check successfull!" mymail.com
rm doku.txt
else
echo "dokuwiki backup fail"
echo -e "dokuwiki backup fail!" | mail -s "dokuwiki backup check failure!" mymail.com
fi
}


fc2(){
$twfunc
if
grep -R "torzon" tw.txt > /dev/null
then
echo "..."
echo "torzon backup successfull"
echo -en "torzon backup success!" | mail -s "torzon backup check successfull!" mymail.com
rm tw.txt
else
echo "torzon backup fail"
echo -e "torzon backup fail!" | mail -s "torzon backup check failure!" mymail.com
fi
}

fc1

fc2

echo "done"

Thanks!

  • Have you tried adding logging to your script to see what's going on? Also, you might want to use explicit paths to the files (e. g. tw.txt that your script references, as cron has a different runtime environment than you do when running scripts manually. You could add set +x to the top of your script, and in the cron table direct stdout and stderr to /tmp/"${0}".log and /tmp/"${0}".err for perusal. – DopeGhoti Dec 2 '14 at 21:40
0

I'm not sure which distribution you're using, but very often cron scripts will run with a minimal shell with less "fanciness" than an interactive shell. Is this your entire script? If so, you should add a shebang to load some proper context.

Also, it helps to use the fully-qualified path for things other than builtin shell commands. For example, find may be at /usr/bin/find. You can use the which command to resolve these:

[me@host ~]$ which find
/usr/bin/find

Ultimately, as DopeGhoti suggested, you should log the output of your cron script somewhere (capturing stdout and stderr). For example,

  0 23 * * * /home/user/backup_script.sh >> /var/log/backup.log 2>&1
  • tried that, doesn't work still sadly. no logging tho – tamani Dec 3 '14 at 17:53
  • @tamani sorry, I don't understand "no logging." Is it possible for you to get the output when cron executes the script? Many versions of cron will mail the user if the job fails. – zje Dec 3 '14 at 19:40

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.