Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

In my crontab file (sudo crontab -e) I have the following commands:

*/1 * * * * wget -q -O /dev/null http://help.mysite.com/inc/mail/hesk_pop3.php
0 7 * * * bash -c "/home/backup/backupscript.sh"

The first line executes and works correctly, but the second line isn't working for some reason. However, If I run the script through terminal (./backupscript.sh) it runs correctly.

The backupscript.sh file is owned by root:root and has 755 permissions.

Within that file is the following bit of code:

#!/bin/bash

/usr/bin/mysqldump -u root -wordpresspassword wordpressusername | /bin/gzip > /home/backup/mysql/wordpress_`date +%m-%d-%Y_%T`.sql.gz
/usr/bin/mysqldump -u root -heskpassword heskusername | /bin/gzip > /home/backup/mysql/hesk_`date +%m-%d-%Y_%T`.sql.gz
/bin/tar cvzf /home/backup/wordpress/wordpress_`date +%m-%d-%Y_%T`.tar.gz /var/www/wordpress/
/bin/tar cvzf /home/backup/hesk/hesk_`date +%m-%d-%Y_%T`.tar.gz /var/www/hesk/
/usr/bin/s3cmd sync -r -P /home/backup/ s3://backups.mysite/
find /home/backup/ -mtime +7 -type f -exec rm {} \;

The script basically backups our MySQL databases and compresses them, backs up our apache sites and compresses them, copies those files to Amazon S3, and then performs a cleanup on the server.

What am I missing here? I've tried a number of things like switching from bash to shell but nothing I've done has worked yet.

share|improve this question
    
Just to make sure: does sudo bash -c "/home/backup/backupscript.sh" work? –  Gertjan Oct 4 '12 at 20:50
    
I'm not familiar with s3cmd but I would assume it requires access to your keyring for upload credentials. This is something that root cannot access if stored in your own keyring. That is just a shot in the dark, could you post the error message from the cron run? –  Simon Déziel Oct 4 '12 at 20:58
    
@Gertjan - Yep, the script runs fine when I run that in terminal. –  Kris Anderson Oct 4 '12 at 21:07
    
@SimonDeziel Yea, s3cmd stores the Amazon key and secret key in a configuration file. We set that up with "s3cmd -config" if I remember correctly. It's processing that command fine and copying the files to S3 as long as I run the script in terminal and not try to automate it using cron. How would I get the error message from cron run? –  Kris Anderson Oct 4 '12 at 21:09
4  
Also, why trying to run this as root if you said your own user could run it no problem? Why not simply add this to your user's crontab? –  Simon Déziel Oct 5 '12 at 16:00
show 5 more comments

2 Answers 2

one workaround is to use shell script to do this:

for (( ; ;))
do
   sleep 86400
   wget -q -O http://help.mysite.com/inc/mail/hesk_pop3.php
   /bin/sh /home/backup/backupscript.sh
done

In above script, I have used sleep 86400 (it's equivalent of one day in seconds). You can configure your interval accordingly.

If you want to run on some server/desktop by logging using ssh, then start/run this script as background process.

share|improve this answer
add comment

General tips for debugging crontab:

  1. catch the output.
    • Either redirect it to a file, e.g. { command1; command2; } &>/tmp/joboutput
    • or ensure that the crond send output as email to your mailbox by setting MAILTO=user@example.com in your crontab.
  2. The shell environment of process invoked by crontab differ from the environment started the same process in a terminal.
    Ensure that all necessary environment variables either specified inside the crontab.
  3. % has a special meaning in the crontab (read carefully man 5 crontab :-)

    The entire command portion of the line, up to a newline or % character, will be executed by /bin/sh or by the shell specified in the SHELL variable of the crontab file. Percent-signs (%) in the command, unless escaped with backslash (), will be changed into newline characters, and all data after the first % will be sent to the command as standard input. There is no way to split a single command line onto multiple lines, like the shell's trailing "\".

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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