[英]Cron job that runs bash script (which runs python script) behaving different than when executed directly
I have a python script that creates a csv and saves it to: 我有一个创建csv并将其保存到的python脚本:
file = example.csv
fileDone = os.path.abspath('/home/bw/temp/%s'%file1)
with open(fileDone, 'w+') as myFile:
a = csv.writer(myFile, delimiter=',')
a.writerow(['login report for %s from %s to %s:\n\n'%(name[0],start_date,end_date)])
dato = ['Username','Logins','Platform']
a.writerow(dato)
for vizL in vizList:
data =[str(vizL[0]),str(vizL[1]),"Viz"]
a.writerow(data)
for appL in appList:
data =[str(appL[0]),str(appL[1]),"Analytics"]
a.writerow(data)
then a bash script calls the python script: 然后bash脚本调用python脚本:
#!/bin/bash
python scriptname.py
REPORT_MONTH=`/bin/date "+%d %B %Y"`
echo -e "Attached is the login reports for $REPORT_MONTH\n\n\n\n\n\nGenerated by: $0" | mutt -a /home/bw/temp/example.csv -c my@email.com -s "Daily Login Reports For $REPORT_MONTH"
Then cron runs the bash script daily. 然后cron每天运行bash脚本。
When I run the bash script manually, everything works fine and the dates are correct. 当我手动运行bash脚本时,一切正常,日期正确。 But when its run by cron, it doesn't seem to overwrite the csv file and just sends one that was already in the /temp folder.
但是,当它由cron运行时,它似乎并不会覆盖csv文件,而只是发送一个已经在/ temp文件夹中的文件。 So I assume it is not regenerating the csv hence not running the python script.
因此,我认为它不会重新生成csv,因此不会运行python脚本。
This is my first time setting up a cron job to run a bash script that runs a python script. 这是我第一次设置cron作业,以运行运行python脚本的bash脚本。 Any potential insight as to why this is happening would be greatly appreciated.
对于为什么发生这种情况的任何潜在见解,将不胜感激。
There are two things that I would try: 我会尝试两件事:
First, try setting the full path to the python script in the bash script, something like this: 首先,尝试在bash脚本中设置python脚本的完整路径,如下所示:
#!/bin/bash
python /home/user/scripts/scriptname.py
REPORT_MONTH=`/bin/date "+%d %B %Y"`
echo -e "Attached is the login reports for $REPORT_MONTH\n\n\n\n\n\nGenerated by: $0" | mutt -a /home/bw/temp/example.csv -c my@email.com -s "Daily Login Reports For $REPORT_MONTH"
Second, if the previous didn't work, it might be a problem with the cron job not having permissions to overwrite the file. 其次,如果以前的版本不起作用,则可能是cron作业没有覆盖文件权限的问题。 Which user is the cron running under?
cron在哪个用户下运行? (it's usually root).
(通常是root)。 Can you post the output of
crontab -l
? 您可以发布
crontab -l
的输出吗? If you remove the file and let the cron create it, does it gets created? 如果删除文件并由cron创建它,是否创建了文件?
First I would make sure your cron is using bash; 首先,我要确保您的cron正在使用bash; by default it uses sh.
默认情况下,它使用sh。 You can include SHELL=/bin/bash to change it.
您可以包含SHELL = / bin / bash进行更改。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.