2

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.

1
  • A possible next step might be to remove all your code from the script and slowly start adding it until your script stops working again. Commented Jan 19, 2018 at 10:54

2 Answers 2

3

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'
Sign up to request clarification or add additional context in comments.

5 Comments

sh: 1: doy: not found Traceback (most recent call last): File "/home/gg/vida.py", line 38, in <module> doynum = '%03d' % (int(line[5])) IndexError: list index out of range This is the output of crontab run but why ? i mean in normal terminal script run it works without any problem.
@GökhanGürbüz it implies that /home/gg/sil.sil doesn't contain the same thing running directly or from cron... Find out what's going on there...
This is the answer you are looking for. You were previously simply losing the error message, and now you are not losing it. It tells you that there is a problem in your code, and now you know.
Maybe point out specifically that putting 2>&1 before the stdout redirection is wrong for this scenario. You want to redirect stderr and stdout to a file, not redirect stdout to the file and then send errors to the old stderr (which apparently isn't connected anywhere useful, though technically, cron will try to send you those messages by email).
Thank you for the pointers. In the end after these modifications it was still giving me errors but mainly it was due to PATH definitions of crontab. The software that i was calling within python required PATH in crontab also. So it is working now. Thank you @AnythingIsFine, JonClements, tripleee.
-1

syntax:

minutes hour dom mon dow user command

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

Comments

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.