Cronjob as root?

1

I'm having a bit of a problem with cronjobs for backups.

I've set up the following in sudo crontab -e (not under personal account):

0 1 * * * /backups/dobackup

/backups/dobackup contains this:

#!/bin/sh
touch ITRAN
tar -cvpjf /backups/$(date +%d.%m.%Y)_backup.tar.bz2  --exclude=/backups --exclude=/proc --exclude=/lost+found --exclude=/sys --exclude=/mnt --exclude=/media --exclude=/dev /

The backup file is created, but the file ITRAN is not. Also, the backup file is vastly smaller than expected:

-rw-r--r--  1 rjrudman root      371620259 2012-06-21 12:39 21.06.2012_backup.tar.bz2
-rw-r--r--  1 rjrudman root     1023211449 2012-06-22 18:00 22.06.2012_backup.tar.bz2
-rw-r--r--  1 rjrudman root        1512785 2012-06-23 01:00 23.06.2012_backup.tar.bz2
-rw-r--r--  1 rjrudman root     1023272455 2012-06-24 22:41 24.06.2012_backup.tar.bz2
-rw-r--r--  1 rjrudman root        1514027 2012-06-25 01:00 25.06.2012_backup.tar.bz2

The backups with much larger file sizes are created by manually running sudo /backups/dobackup. It seems the cronjob is failing at some point.. but I have no idea how to debug this issue or where to start.

Any ideas? Running ubuntu 10.04

Rob

Posted 2012-06-24T23:47:37.740

Reputation: 113

1You can always redirect stdout & stderr to find out if tar fails, as well as the value of pwd to see where ITRAN is created. – mlt – 2012-06-24T23:57:45.440

@mlt Cheers, I'll give it a go and have a look next time it runs – Rob – 2012-06-25T00:20:42.640

BTW, instead of using crontab -e you could place a file in /etc/cron.d/, the file format is slightly different and allows you to set the username you want. See the existing files in that directory for examples. – Zoredache – 2012-06-25T01:04:23.463

Answers

4

If you want to find out why it's failing, try logging the output of your backup script:

0 1 * * * /backups/dobackup >& /root/dobackup.log

paddy

Posted 2012-06-24T23:47:37.740

Reputation: 1 119

Cheers - I've set that up - is there any way to trigger a cronjob immediately? Other than setting up a new one a few minutes from now? – Rob – 2012-06-25T00:31:21.087

1Simple answer is no: just schedule one to execute in the next minute. But nothing would stop you from creating a fairly simple program to allow arbitrary scripts to be executed by cron. You'd run it as * * * * *, and have it poll a control queue file for changes every second until 60 seconds had elapsed, then exit. If something showed up in the queue it would be executed and then removed from the queue (you'd need file locking to prevent multiple executions). You would use this same program (with different arguments) to add entries to the queue. That's what I'd do anyway... =) – paddy – 2012-06-25T02:23:28.400

2

Crontab environment is not the same environment as bash terminal. You need to program commands for crontab seperateley.

blogger

Posted 2012-06-24T23:47:37.740

Reputation: 590

Is there any way I can emulate the crontab environment and run it immediately for testing? – Rob – 2012-06-25T00:13:12.850

1@Rob You should use full paths. Instead of touch and tar use the full path, like /usr/bin/touch. To find the location of the program, use whereis touch and same for tar. – Devator – 2012-06-25T00:48:04.710

1

One thing I have learned is to figure out what the cron environment has access to.

This may not be a solution but it could open your eyes to this issue in the future.

Have cron execute this command:

* * * * *  set > /temp/text/file

Take a look at it and compare it to that of a normal bash shell.

Vastly different and it will cause unseen problems

steve

Posted 2012-06-24T23:47:37.740

Reputation: 612