Skip to content Skip to sidebar Skip to footer

Crontab Python Subprocess And Service Restart

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)

Solution 1:

Try calling the command using absolute path, as you call it without shell and under another user account, some commands are not available without specifying absolute path.

First find, where is the command located:

$ which service
/usr/sbin/service

Then change your code to:

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

Post a Comment for "Crontab Python Subprocess And Service Restart"