crontab does not run my python script

My python script is not running under my Crontab. But when i try to run it from the Terminal it works perfectly. I have placed this in the python script at the top:

#!/usr/bin/python

Also I tried:

#!/usr/bin/env python

I did my file executable:

chmod a+x vida.py

Added to my crontab and added PATH:

USER=gg
SHELL=/bin/sh
PATH=/usr/local/sbin/:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin/:/home/gg/DC.bin/:/home/gg/GNSSMET/DC/:usr/bin:/usr/bin/X11:/:/home/gg/GNSSMET/DC/bin/:/home/gg/:/usr/lib/python2.7/:
PYTHONPATH=/usr/bin/:/usr/lib/python2.7/

*/1 *  * * *    gg   /usr/bin/python /home/gg/vida.py 2>&1 >>/home/gg/out1.txt

I checked the log via grep CRON /var/log/syslog

Jan 19 13:37:01 gg-pc CRON[26500]: (gg) CMD ( /usr/bin/python /home/gg/vida.py 2>&1 >>/home/gg/out1.txt)

I even run a dummy python script from using crontab and it worked like charm (simple Hello, World!). But when it comes to my script the output file out1.txt is created (which is empty) but does not run the actual script. I even checked all of the solutions presented on StackOverflow, none did work. So here is my python script:

#!/usr/bin/env python

from datetime import *
import os
import sys

gamitRinexDir = '/home/gg/GAMIT/rinex'
stalist = ['ankr','argi','aut1','beug','brst','bucu','busk','ganm','gism','glsv','gmlk','gope','hofn','ingl','ista','joze',
'kiru','krcy','ktvl','mas1','mate','mets','mkps','morp','nico','onsa','orhn','orid','pdel','penc','polv','pots','puyv',
'sofi','vis0','vlns','wtzr','yebe','zeck','zimm']

letlist = ['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X']
seslist = ['0','1','2','3','4','5','6','7','8','9']
tnow  = datetime.now()
dback = timedelta(hours=2)
tnow  = tnow -dback
wlength = 4

os.system('rm ' + gamitRinexDir + '/*')
wlett   = []
updir   = []
doylist = []
yrlist  = []
for i in range(wlength):
   delta = timedelta(hours=i+1)
   tback = tnow -delta
   wlett.append(letlist[tback.hour])
   doystr = 'doy ' + str(tnow.year) + ' ' + str(tnow.month) + ' ' + str(tnow.day) + ' ' + '> /home/gg/sil.sil'
   os.system(doystr)
   fid = open('/home/gg/sil.sil')
   line  = fid.readline().split()
   doynum = '%03d' % (int(line[5]))
   x      = str(tnow.year)
   yrnum  = x[2:4]
   updir.append(yrnum+doynum)
   doylist.append(doynum)
   yrlist.append(yrnum)

dirname = '/home/gg/REPO/nrtdata/'
for i in range(len(wlett)):
    adirname = dirname + updir[i]+'/' + wlett[i]
    for sta in stalist:  
        fname  = adirname      + '/' + sta + doylist[i] + wlett[i].lower() + '.' + yrlist[i]+'d.Z'
        fname2 = gamitRinexDir + '/' + sta + doylist[i] + seslist[i]       + '.' + yrlist[i]+'d.Z'
        os.system('cp ' + fname + ' ' + fname2)

udoy = list(set(doylist))
dcmd = ''
for gun in udoy:
    dcmd = dcmd + gun + ' '    

CmdGamit = 'sh_gamit -d ' + x + ' ' + dcmd + ' ' + '-orbit IGSU -expt expt -eops usnd -gnss G -nopngs -metutil Z'
print(CmdGamit)
mainCmd = 'cd /home/gg/GAMIT/;'+CmdGamit
os.system(mainCmd)
filestocopy1 = 'met_*'
filestocopy2 = 'hexpta.*'
filestocopy3 = 'uexpt*'
ndirname = ' /home/gg/REPO_GAMIT/' + doynum + '_'+ wlett[-1]
os.system('mkdir ' + ndirname)
cleancmd1 = 'mv /home/gg/GAMIT/'+doynum +'/'+filestocopy1 + ' ' + ndirname
cleancmd2 = 'mv /home/gg/GAMIT/'+doynum +'/'+filestocopy2 + ' ' + ndirname
cleancmd3 = 'mv /home/gg/GAMIT/'+doynum +'/'+filestocopy3 + ' ' + ndirname
cleancmd4 = 'rm -r /home/gg/GAMIT/'+doynum
os.system(cleancmd1)
os.system(cleancmd2)
os.system(cleancmd3)
os.system(cleancmd4)

Please show me some pointers, I am seriously stuck here.

Upvotes: 2

Views: 7094

Answers (2)

AnythingIsFine
AnythingIsFine

Reputation: 1807

You should change you crontab line as such to get stdout and stderr saved to the file:

*/1 *  * * *    gg   /usr/bin/python /home/gg/vida.py >> /home/gg/out1.txt 2>&1

Simply read out1.txt after crontab has run the line to see what's wrong

Edit after your comment: Based on the error you've shared, I believe you're not actually writing anything in the /home/gg/sil.sil file:

doystr = 'doy ' + str(tnow.year) + ' ' + str(tnow.month) + ' ' + str(tnow.day) + ' ' + '> /home/gg/sil.sil'
os.system(doystr)

doystr does not evaluate to a shell command, I think you need to write the variable as below to write to the file.

doystr = 'echo "doy ' + str(tnow.year) + ' ' + str(tnow.month) + ' ' + str(tnow.day) + '" ' + '> /home/gg/sil.sil'

Upvotes: 3

Vinith Kumar
Vinith Kumar

Reputation: 30

syntax:

minutes hour dom mon dow user command

55 16 * * * root /root/anaconda/bin/python /root/path/file_name.py &>> /root/output/output.log

Upvotes: -1

Related Questions