Reputation: 126
I am trying to run the code below as cronjob without any luck...
import sys
import time
import tarfile
def main(argv):
#f = open('/tmp/backup-log.txt', 'a')
#f.write('variable start\n')
timeStamp = time.strftime('%y%m%d')
nagiosFolder = '/app/nagios/'
fileName = '/app/nagios_install/backup/nagios-backup-%s.tar.gz' % timeStamp
#f.write('variable end\n')
try:
#f.write('tar start\n')
tarGeza = tarfile.open(fileName, 'w:gz')
tarGeza.add(nagiosFolder)
tarGeza.close()
#f.write('tar end\n')
#f.close()
sys.exit(0)
except tarfile.TarError, tarexc:
#f.write('exception error')
#f.close()
print tarexc
sys.exit(1)
if __name__ == '__main__':
main(sys.argv[1:])
The commented sections are for debbuging purposes and whenever the code runs, it shows as the code has finished without errors:
variable start
variable end
tar start
tar end
My crontab settings are:
HOME=/usr/nagios/
LOGNAME=nagios
PATH=/usr/lib64/qt-.3/bin:/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin:/usr/bin/python
SHELL=/usr/bin/python
17 12 * * * /usr/bin/python /app/nagios_install/backup/nagios_backup.py
And permissions are the following:
-rwxrwxr-x 1 nagios root 1009 Jan 17 11:00 /app/nagios_install/backup/nagios_backup.py
Can anyone please highlight what I might be doing wrong? Thanks in advance!
Upvotes: 1
Views: 3821
Reputation: 76997
Just some tips from my end - how I would try to schedule the whole thing
1) Include a shebang
line at the top of the python script for picking up the python executable and remove the python executable path from the cron
entry - there is always the off chance that the paths for the executables are different on productions server from development environment.
#!/usr/bin/env python
2) Change the mode of the script to 755
to make the script executable
sudo chmod 755 /app/nagios_install/backup/nagios_backup.py
3) Schedule the cron job from the root user's crontab
sudo crontab -e
crontab -e
opens up current user's crontab by default, and not root user's crontab. And the nagios
directory may not be acceessible by that current user.
4) Remove the SHELL
variable from your crontab, it isn't needed in the first place. You are not using variables LOGNAME
, HOME
either, so they can also be removed, I don't think they were needed either.
5) Schedule the cron job like following in your root user's crontab
17 12 * * * /app/nagios_install/backup/nagios_backup.py >> /var/log/nagios_backup.log 2>&1
I think the above setup should work. If it doesn't, try running the script directly and let me know what error(s) it throws.
Upvotes: 4