I recently faced a weird issue where I had a Python Script when run in my normal shell prompt as ./shell_script.sh runs fine, but when I run it as a cron job, it fails. After a lot of digging around I found out that the culprit was an OS command running in the python script.

I was running the following command in the script to get the IP Address of the eth0 interface and this script abruptly exits when run as a cron job.
ifconfig_output_stream = os.popen("ifconfig eth0|grep inet\ addr")

The problem was that crontab does not load the PATH variables that are normally loaded when you login to a shell prompt of the instance. So when the above line of code was being executed from crontab, the python script exited without any errors since ifconfig command requires /usr/sbin in the path variable.

The solution was to load the required PATH variables in crontab which were as set in the shell prompt when logging in. So now I had the first line in my crontab which looks like this

test_user@ip-10-24-250-215:~$ crontab -l
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
46 * * * * /home/user/shell_script.sh

Once I had this figured out, the second part was how to add this on all the instances through fabric. sed command is very helpful to add text to a file form the command line to the begging, end or replace existing content.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
    def add_path_to_beginning_of_crontab (self):            
        if (not os.path.exists('/home/user/backup_cron')):
            run ('mkdir /home/user/backup_cron')
 
        with cd('/home/user/backup_cron'):
            corn_file = "crontab_%s" % str (datetime.datetime.now())
            corn_file = corn_file.replace (" ","_")
            corn_file = corn_file.replace (":","_")
            corn_file = corn_file.replace (".","_")
            run ('crontab -l > %s' % corn_file)
            # Append to the beginning of the file
            # Here the sed command is very useful to append to beginning of the
            # file from the command line
            run ('sed -i \'1i\\PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin\' %s' % corn_file)
            result = run ('crontab %s' % corn_file)
 
            if result.return_code != 0:
                print "Crontab installation failed"
                print "Output:  %s"%(result)
 
            else:
                print 'Crontab installed sucessfully'
                print "Output:  %s"%(result)
	def add_path_to_beginning_of_crontab (self):			
		if (not os.path.exists('/home/user/backup_cron')):
			run ('mkdir /home/user/backup_cron')

		with cd('/home/user/backup_cron'):
			corn_file = "crontab_%s" % str (datetime.datetime.now())
			corn_file = corn_file.replace (" ","_")
			corn_file = corn_file.replace (":","_")
			corn_file = corn_file.replace (".","_")
			run ('crontab -l > %s' % corn_file)
			# Append to the beginning of the file
			# Here the sed command is very useful to append to beginning of the
			# file from the command line
			run ('sed -i \'1i\\PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin\' %s' % corn_file)
			result = run ('crontab %s' % corn_file)

			if result.return_code != 0:
				print "Crontab installation failed"
				print "Output:  %s"%(result)

			else:
				print 'Crontab installed sucessfully'
				print "Output:  %s"%(result)
Follow on Google+ LinkedIn Updated on February 12, 2014 at
Post your comment

All the fields must be filled out before posting.