如何在 cron 作业中将 bash 参数传递给 python 脚本?
我有一个 python 应用程序,它使用 getopt() 来解析命令行中的参数。它在交互式 bash shell 中或在从命令行调用的 bash 脚本中运行良好,但在通过 cron 从 bash shell 脚本中调用时它不会执行。我使用的是 Fedora 15。
当作为 cron 作业调用时,应用程序失败,在 /var/log/messages 中发出以下错误:
myscript.py: abrt:在 myscript.py 中检测到未处理的 Python 异常
似乎命令行参数没有正确传递给 python 脚本。
例如,以下命令行调用可以正常工作,将输入文件名设置为“input.txt”并将“log”和“timer”标志设置为所需的值:
python myscript.py -i input.txt --log --timer
当我尝试通过位于命令行下,bash 脚本工作正常。但是当我尝试通过 cron 运行 bash 脚本时,执行失败并出现上述错误。
我无法确定为什么 shell 脚本无法通过 cron 正确执行。我使用的是用于 cron 作业的 bash 脚本中的完整路径名,因此环境路径不应该成为问题:
/usr/bin/python /path/to/myscript.py -i /path/to /input.txt --log --timer
我认为这种语法在通过 cron 调用的 bash 脚本中使用时,可能会将脚本的参数传递给 python,而不是 myscript.py。
任何帮助将不胜感激!
I have a python application that uses getopt() to parse the command line for parameters. It works fine in an interactive bash shell, or in a bash script that is called from the command line, bit it will not execute when being called from within a bash shell script via cron. I am using Fedora 15.
The application fails when called as a cron job, issuing the following error in /var/log/messages:
myscript.py: abrt: detected unhandled Python exception in myscript.py
It seems that the command line parameters aren't being passed to the python script properly.
For example, the following command line invocation works properly, setting the input filename to "input.txt" and setting the "log" and "timer" flags to their desired values:
python myscript.py -i input.txt --log --timer
When I attempt to invoke the program via a bash script at the command line, the bash script works fine. But when I attempt to run the bash script via cron, execution fails with the aforementioned error.
I am at a loss to determine why the shell script fails to execute properly via cron. I am using full path names from within the bash script used for the cron job, so environment paths shouldn't be an issue:
/usr/bin/python /path/to/myscript.py -i /path/to/input.txt --log --timer
I am thinking that this syntax, when used in a bash script called via cron, might be passing the script's parameters to python, rather than to myscript.py.
Any help would be appreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
虽然我同意这两条评论,但调试 crontab 条目问题的一种方法是确保您收到所有错误消息。您正在查看 cron 用户电子邮件文件吗?任何未捕获的 cron 输出都会作为电子邮件发送到用户 ID。 (如果用户在 cron'ed 机器上没有电子邮件帐户,那是一个单独的问题)。
无论如何,我发现显式捕获输出很有帮助。如果我没有得到输出文件(至少是零大小的文件),那么我知道 crontab 甚至没有运行。
下面是一个设置为捕获所有输出的 crontab 条目示例。
回想一下(根据 @cji 的提醒), cron 将使用 /bin/sh (在系统上最终可能会是 bash,因此您在命令行上包含的任何变量分配最好像
69 13 05 06 那样完成* { var=1 ; export cmds ... } ...
我从来没有机会从 crontab 中获取环境变量文件,但我希望它能与
./path/to/envFile
.
While I agree with both comments, one way to debug issues with crontab entries is to be sure you're getting all the error messages. Are you looking at the cron users email file? Any un-captured output from cron is sent as an email to the userID. (If the user doesn't have an email account on the cron'ed machine, that is a seperate problem).
In any case, I find it helpful to explicitly capture the output. If I don't get output files, (at least zero sized files), then I know the crontab didn't even run.
Here's a sample crontab entry set up to capture all the output.
Recall (per @cji 's reminder), that cron will use /bin/sh (which on systems may wind up being bash anyway, so any variable assingments you include on the command line are better done like
69 13 05 06 * { var=1 ; export var ; cmds ... ; } ...
I've never had occasion to 'source' an environment variables file from a crontab, but I would expect it to work with
. /path/to/envFile
.IHTH