1

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!

3
  • Just to clear the initial post...I am able to run the code from CLI, however when it is scheduled via crontab nothing seems to happen.. Commented Jan 19, 2014 at 22:10
  • You should redirect the output of your cronjob to a log file to capture error messages, for example 17 12 * * * /usr/bin/python /app/nagios_install/backup/nagios_backup.py >> /var/log/nagios_backup.log 2>&1 Commented Jan 19, 2014 at 22:14
  • 2
    For starters, you certainly don't want the SHELL for your crontab to be /usr/bin/python, because your command won't make any sense to a python shell (/usr/bin/python /app/nagios_install/backup/nagios_backup.py). If that doesn't suffice, you should drop the output in a log file. Commented Jan 19, 2014 at 22:14

1 Answer 1

4

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.

Sign up to request clarification or add additional context in comments.

1 Comment

This is a very detailed answer! Removing the variables and using the root's crontab did resolve the issue. Thanks very much for your help ansh0l!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.