我有下面的python测试脚本(从一个更大的.py文件中提取),我正试图在EC2实例(haproxy OpsWorks实例)上工作。
在作为cron条目运行时应用的权限与作为来自shell的python脚本运行时应用的权限之间似乎有一个主要区别。
我在其他文章中找到的最接近的是here,但这与shell脚本有关,并且,考虑到我运行python OK,路径应该(我希望)为cronjob进行充分配置。
环境: -Python版本:2.6.8 -环境:OpsWorks HAProxy实例,在EC2上。 -以用户身份运行:根。
脚本
import subprocess
import boto
import logging
if __name__ == '__main__':
logging.getLogger().setLevel(boto.logging.INFO)
command = ['service', 'haproxy', 'reload'];
logging.info('Executing: %s' % command)
#shell=FALSE for sudo to work.
subprocess.call(command, shell=False)
我也尝试过使用以下命令调用subprocess命令,但没有成功。
# subprocess.call("%s %s %s %s" % ('sudo', 'service', 'haproxy', 'reload'))
从命令行运行时输出:
[root@lb1 ~]# python tester.py
INFO:root:Executing: ['service', 'haproxy', 'reload']
Reloading haproxy:
[root@lb1 ~]#
Crontab条目:
[root@lb1 ~]# crontab -l
*/1 * * * * python ~/tester.py > ~/testlog 2>&1
作为crontab命令运行时的输出
[root@lb1 ~]# cat testlog
INFO:root:Executing: ['service', 'haproxy', 'reload']
Traceback (most recent call last):
File "/root/tester.py", line 13, in <module>
subprocess.call(command, shell=False)
File "/usr/lib64/python2.6/subprocess.py", line 478, in call
p = Popen(*popenargs, **kwargs)
File "/usr/lib64/python2.6/subprocess.py", line 639, in __init__
errread, errwrite)
File "/usr/lib64/python2.6/subprocess.py", line 1228, in _execute_child
raise child_exception
OSError: [Errno 2] No such file or directory
我搞不懂它到底为什么会抛出“没有这样的文件或目录”的错误!
有人能给我指点一下吗?顺便说一下,这是一个更大的EC2集成部分的一部分,因此boto导入(这是一个在这个实例中获得日志记录的好方法)。它是以根用户身份运行的,因为从浏览的角度来看,使用sudo命令运行用户帐户的cron条目是不好的做法。
干杯
事实证明,这确实与路径有关。
Path Information for Cronjobs
Commands in submodules
为了解决这个问题,我通过
crontab -e
提供了以下条目现在我的服务按预期重新启动。耶!
在子进程集中
shell=True
我认为你的问题会得到解决,但sudo问题可能会出现。
相关问题 更多 >
编程相关推荐