Cannot get crontab to work

I try to get crontab to work for a while, but it doesn't seem to want to work. Python script I need to initialize every midnight, work fine with the command terminal. Location of my python script:

/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 this:

 #!/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 I'm wrong in?


Just looked at the cron logs and I got this for every entry I tried

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

I have the same error when I tried with a shell script, and

+4
source share
4 answers

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 put this in your crontab:

 0 0 * * * /PATH/TO/mysql_backup.sh 
+1
source

If it works from your user account but not from cron, it is usually not cron. The cron daemon is trying to start your application, but since it has not configured environment variables, it will not work. Create a simple shell script to set up environment variables and run your python script. (You do not have PATH, so you should use full path names, etc.)

Looking at your script, you can do all this quite easily using only shell script commands. Shell scripts are significantly underestimated.

+1
source

you can just check crontab cmd and execute these cmd manually, for example.

 /usr/bin/scrond run-parts /etc/cron.daily 
+1
source

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

Of course, this also introduces another point of failure, which is that you do not have write permission to where you put the errors.txt file .; -)

0
source

Source: https://habr.com/ru/post/1313546/


All Articles