简体   繁体   English

与直接执行时不同的运行bash脚本(运行python脚本)的Cron作业

[英]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.

相关问题 Python脚本在bash中运行,但不在cron中运行? - Python script runs in bash, but not in cron? Cron执行的python脚本运行但不更新sqlite db - Cron executed python script runs but does not update sqlite db python脚本在共享主机ssh中运行,但不在cron作业中运行 - python script runs in shared hosting ssh, but not in cron job Cron作业无法运行从控制台运行良好的bash脚本(需要xsession的shell python脚本) - Cron job can't run bash script (that shells python script requiring xsession) that runs perfectly well from console 使用Pyinstaller将运行bash脚本的python脚本转换为可执行文件 - Convert python script which runs a bash script to executable file with Pyinstaller cron 调用 bash 脚本,该脚本调用“脚本”来运行 python 程序。 bash 脚本的下一行在 script/python 完成之前立即运行 - cron invokes bash script which invokes “script” to run python program. Next line of bash script runs immediately, before script/python completes 脚本在终端上运行,但不是 cron - Script runs on terminal, but not as cron 在shell中运行python的脚本 - The script which runs python in shell 多次执行时,Python脚本运行速度较慢 - Python script runs slower when executed multiple times 当脚本从 Cron Job 运行时如何在脚本目录中打开文件 - How to open file in script directory when script runs from Cron Job
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM