简体   繁体   中英

Tar not working from cronjob

I have a python script that backups the sql database when run.

It works flawlessly when i ruin it manually but when it does not run the tar and rm command!

before the backup i run the mysqldump command ofcourse and then after i run this. This is the part of the script that deals with the backup

os.system('tar -cvzf Backup_' + formt + '.tar.gz ' + 'sql_Backup_' + formt + '.sql')
os.system('rm ' + 'sql_Backup_' + formt + '.sql')

in the cronjob i have

# Python SQL Backup Script
00 23 * * 07 sudo /usr/bin/python /home/user/scripts/SQLbackup.py

When i run this from the shell itself it runs fine sudo python /home/user/scripts/SQLbackup.py

but the cronjob does NOT. it only runs the script and creates the sql file and thats it. It doesnt compress it and it does not remove the sql file after compressing either.

i added this to the sudoers file

root ALL=(ALL) NOPASSWD:/bin/tar
sudo ALL=(ALL) NOPASSWD:/bin/tar

and still NOTHING. I put both root and sudo just to be 100 percent sure.

Keep in mind: It runs perfect manually but does NOT run from crontab fully.

The problem might be that $PATH isn't set correctly when the script is run from crontab which than causes the script to not find the specified binaries.

Also check that the script is executed in the right directory.

Modify the script to include the full path to tar and rm and test if it works better:

os.chdir('/path/to/where/sqlfile/resides')
os.system('/usr/bin/tar -cvzf Backup_' + formt + '.tar.gz ' + 'sql_Backup_' + formt + '.sql')
os.system('/bin/rm ' + 'sql_Backup_' + formt + '.sql')

Note that tar usually is installed in /usr/bin but that can differ between different unix flavours.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM