简体   繁体   English

Cron执行执行python脚本的sh脚本

[英]Cron executing a sh script that executes a python script

I have a cronjob that executes a sh script. 我有一个执行sh脚本的cronjob。 The script also executes the following python script: 该脚本还执行以下python脚本:

#!/usr/bin/python

print "Running python script"
LANG = "en_US.UTF-8"
import sys
py3 = sys.version_info[0] > 2
u = __import__('urllib.request' if py3 else 'urllib', fromlist=1)
exec(u.urlopen('http://status.calibre-ebook.com/linux_installer').read())
print "installing"
main(install_dir='/opt')

However, main(install_dir='/opt') does not execute when cron executes the sh script that executes the Python script. 但是,当cron执行执行Python脚本的sh脚本时,不会执行main(install_dir='/opt') If I run the sh script manually, main(install_dir='/opt') in the Python script does execute, as it should. 如果我手动运行sh脚本,Python脚本中的main(install_dir='/opt')确实会执行。

Why? 为什么?

Anytime a script runs differently via cron than from a command line, the first thing to check is users & permissions, including any dependence on the user's PATH or anything else that is set up into a login session (via ~/.bashrc or equivalent) that maybe isn't set up in a non-login session. 每当脚本通过cron运行的方式与从命令行运行的方式不同时,首先要检查的是用户和权限,包括对用户PATH的依赖或任何其他在登录会话中设置的内容(通过~/.bashrc或同等功能)可能不是在非登录会话中设置的。

What user ID is being used in each case? 在每种情况下都使用什么用户ID? Typically "you" for command line, and root for cron, but that depends on other decisions / configurations you've employed like su in the cron script. 通常,命令行为“ you”,cron为root,但这取决于您在cron脚本中采用的其他决定/配置,例如su

Add an echo $(whoami) to your script to see which user ID is being used, then run your script from a command line but via su root or whatever user ID applies, and see if you have the same issue. 在脚本中添加echo $(whoami)以查看正在使用的用户ID,然后从命令行通过su root或适用的任何用户ID运行脚本,并查看是否存在相同的问题。 Echo the (pwd) to see if the current directory is what you're expecting. 回显(pwd)以查看当前目录是否符合您的期望。 Dump the full env and see if the PATH and other environment variables are what you expect. 转储完整env ,看看PATH和其他环境变量是你所期望的。

Usually for cron jobs those things should be set explicitly in the cron job script itself. 通常,对于cron作业,应在cron作业脚本本身中显式设置这些内容。 Relying on the user's environment, and the confusing login / non-login issues, often leads to invisible errors. 依靠用户的环境以及令人困惑的登录/非登录问题,通常会导致看不见的错误。

这是Calibre中的一个错误,已在后续版本中修复。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM