crontab不运行我的python脚本

时间:2018-01-19 10:50:21

标签: python cron

我的python脚本没有在我的Crontab下运行。但是,当我尝试从终端运行它时,它完美地工作。我把它放在顶部的python脚本中:

#!/usr/bin/python

我也试过了:

#!/usr/bin/env python

我做了我的文件可执行文件:

chmod a+x vida.py

添加到我的crontab并添加了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

我通过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)

我甚至使用crontab运行一个虚拟python脚本,它就像魅力(简单的Hello,World!)。但是当涉及到我的脚本时,输出文件out1.txt被创建(它是空的)但不运行实际的脚本。我甚至检查了StackOverflow上提供的所有解决方案,没有一个工作。 所以这是我的python脚本:

#!/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)

请给我一些指示,我严重地被困在这里。

2 个答案:

答案 0 :(得分:1)

您应该更改crontab行,以便将stdoutstderr保存到文件中:

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

在crontab运行该行以查看错误后,只需阅读out1.txt

评论后编辑: 根据您分享的错误,我相信您实际上并没有在/home/gg/sil.sil文件中写任何内容:

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

doystr不评估shell命令,我认为你需要编写如下变量来写入文件。

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

答案 1 :(得分:0)

语法:

分钟小时dom mon dow用户命令

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