views:

213

answers:

4

I've been trying to get crontab to work for a while but it doesnt seem to want to work. The python script I need to initialise every midnight works perfectly from the command terminal. The location of my python script is:

/home/rv/ncbi-blast-2.2.23+/database_backup/backup.py

My contab looks like this:

SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin:/home/rv/ncbi-blast-2.2.23+/database_backup
MAILTO=root
HOME=/
# For details see man 4 crontabs

# Example of job definition:
# .---------------- minute (0 - 59)
# |  .------------- hour (0 - 23)
# |  |  .---------- day of month (1 - 31)
# |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...
# |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# |  |  |  |  |
# *  *  *  *  *  command to be executed
0 0 * * * /usr/bin/python /home/rv/ncbi-blast-2.2.23+/database_backup/backup.py

My python script looks like the following:

#!/usr/bin/python

from subprocess import Popen
import datetime

today = datetime.date.today()

today = str(today)

#print today

f = open("/home/rv/ncbi-blast-2.2.23+/database_backup/%s.sql" % (today), "w")
x =  Popen(["mysqldump", "-u", "root", "-p*****", "normalisation"], stdout = f)
x.wait()
f.close()

Any idea where im going wrong?


Just looked at the cron logs and i got this for each time entry I tried

(root) BAD FILE MODE (/etc/crontab)

I got the same error when i tried with a shell script aswell

A: 

I don't have an idea as to where you are going wrong, but you can redirect stderr and stdout to a file in your crontab entry which might give you a hint. My syntax memory is rusty but it's something like <yourfile>.py &> errors.txt

Of course, this also introduces another failure point, which is that you don't have permission to write to wherever you are putting errors.txt. ;-)

Bo Williams
+1  A: 

If it works from your user account but not from cron it's usually not cron. The cron daemon tries to start your application but since it doesn't set up the environment variables it will fail. Create a simple shell script to setup your environment variables and starts your python script. (You don't have a PATH so you must use full path names, etc.)

Looking at your script you can do all this pretty easily using just shell script commands. Shell scripting is vastly under-rated.

Jay
I agree, for stuff like this Python just makes it wordier. A bash script is much better suited. And this is probably the correct answer; PATH issues are the #1 culprit of cron jobs not working properly. I usually just use full paths in cron jobs to avoid hassle.
Ibrahim
A: 

Perhaps instead of using Python, make a bash script:

mysql_backup.sh

#!/bin/bash
/PATH/TO/mysqldump -u root -p***** normalisation > /SOMEOTHER/PATH/TO/$(date '+%Y-%m-%d').sql

and place this in your crontab:

0 0 * * * /PATH/TO/mysql_backup.sh 
unutbu
silly question, where would i put the script?
Craig
You could put the script in `/usr/local/sbin` where locally installed programs for system administration typically go.
unutbu
I tried with a shell script but again no luck, had a look at the cron logs and had this:"(root) BAD FILE MODE (/etc/crontab)"
Craig
According to http://www.ehow.com/how_4397618_bad-file-mode-cron-errors.html, `BAD FILE MODE` error may occur if `/etc/crontab` does not have the right permissions. Perhaps try `sudo chmod 644 /etc/crontab` and `sudo chown root:root /etc/crontab`.Also -- if you are using the system-wide /etc/crontab, not a personal crontab, then the format for crontab is a little different. Use`0 0 * * * root /PATH/TO/mysql_backup.sh` instead. The 6th field (`root`) specifies the user who runs the script. See `man 5 crontab`, the section entitled `EXAMPLE SYSTEM CRON FILE`.
unutbu
That solved it thanks
Craig
@Craig: Glad it worked.
unutbu
A: 

Have you checked /var/log/cron? What does it indicate?

frankc