Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/azure/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
crontab python子流程和服务重启_Python_Linux_Cron - Fatal编程技术网

crontab python子流程和服务重启

crontab python子流程和服务重启,python,linux,cron,Python,Linux,Cron,我有一个小python代码,可以在不存在的情况下重新启动nginx。 当我运行sudo python monitor\u server.py时,一切正常。 当我试图用根cron(sudo crontab-e)对其进行cron时,行为: ***python/root/monitor\u server.py>/var/log/my\u monitor/cron\u log.log 2>&1 我得到: 回溯(最近一次呼叫最后一次): 文件“/root/monitor_server.py”,第19行,在

我有一个小python代码,可以在不存在的情况下重新启动nginx。 当我运行
sudo python monitor\u server.py
时,一切正常。 当我试图用根cron(
sudo crontab-e
)对其进行cron时,行为:
***python/root/monitor\u server.py>/var/log/my\u monitor/cron\u log.log 2>&1
我得到:

回溯(最近一次呼叫最后一次):
文件“/root/monitor_server.py”,第19行,在
重新启动\u服务(“mongod”)
文件“/root/monitor\u server.py”,第10行,在restart\u服务中
subprocess.call(命令,shell=False)
文件“/usr/lib64/python2.6/subprocess.py”,第478行,在调用中
p=Popen(*popenargs,**kwargs)
文件“/usr/lib64/python2.6/subprocess.py”,第642行,在__
错误读取,错误写入)
文件“/usr/lib64/python2.6/subprocess.py”,第1234行,在_execute_child中
引发子对象异常
OSError:[Errno 2]没有这样的文件或目录
守则:

def restart_service(name):
    command = ['service', name, 'restart'];
    #shell=FALSE for sudo to work.
    subprocess.call(command, shell=False)

if __name__ == '__main__':
    try:
        f = urllib2.urlopen("<healthcheck URL>")
    except (urllib2.HTTPError, urllib2.URLError) as e:
        logging.log(logging.ERROR, 'restarting server')
        restart_service('nginx')
def重启_服务(名称):
命令=['service',name','restart'];
#shell=FALSE,sudo可以工作。
subprocess.call(命令,shell=False)
如果uuuu name uuuuuu='\uuuuuuu main\uuuuuuu':
尝试:
f=urllib2.urlopen(“”)
除了(urllib2.HTTPError,urllib2.urleror)作为e:
logging.log(logging.ERROR,“重新启动服务器”)
重新启动\u服务(“nginx”)

尝试使用绝对路径调用该命令,因为您在没有shell的情况下调用它,并且在另一个用户帐户下,如果不指定绝对路径,某些命令将不可用

首先查找,命令位于何处:

$ which service
/usr/sbin/service
然后将代码更改为:

def restart_service(name):
    command = ['/usr/sbin/service', name, 'restart'];
    #shell=FALSE for sudo to work.
    subprocess.call(command, shell=False)

尝试使用绝对路径调用
服务
,如
usr/sbin/service
。使用代替cron。@JanVlcinsky-没有帮助,相同result@ChristianBerendt-试着让moni工作几个小时。无论我做什么,它都没有重新启动我的nginxtried@Boaz很抱歉输入错误,应该有前导斜杠,所以您应该尝试
/usr/sbin/service
(或者
哪个服务返回的内容)。无论如何,我不保证,这会奏效,只是一个想法。
def restart_service(name):
    command = ['/usr/sbin/service', name, 'restart'];
    #shell=FALSE for sudo to work.
    subprocess.call(command, shell=False)