I have a small python code that restarts nginx on it's not existing.
When I run sudo python monitor_server.py
all is fine.
When I tried to cron it with root cron (sudo crontab -e
) with the line:
* * * * * python /root/monitor_server.py > /var/log/my_monitor/cron_log.log 2>&1
I get:
Traceback (most recent call last):
File "/root/monitor_server.py", line 19, in <module>
restart_service('mongod')
File "/root/monitor_server.py", line 10, in restart_service
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 642, in __init__
errread, errwrite)
File "/usr/lib64/python2.6/subprocess.py", line 1234, in _execute_child
raise child_exception
OSError: [Errno 2] No such file or directory
The code:
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')