I am trying to make a
crontab to start
nginx if it is stopped.
I googled and found these two scripts
#!/bin/bash service=replace_me_with_a_valid_service if (( $(ps -ef | grep -v grep | grep $service | wc -l) > 0 )) then echo "$service is running!!!" else /etc/init.d/$service start fi
somehow if I run it manually it works fine by doing
source scriptName after I add it to
crontab even if the service is stopped, it keeps on echoing
nginx is running and does not start the service.
Then I found another script in digital ocean
#!/bin/sh ps auxw | grep nginx | grep -v grep > /dev/null if [ $? != 0 ] then /etc/init.d/nginx start > /dev/null fi
again if I run it manually it works, but it will ask for the user's password
==== AUTHENTICATING FOR org.freedesktop.systemd1.manage-units === Authentication is required to start 'nginx.service'. Authenticating as: abc,,, (abc)
after I typed in the password
==== AUTHENTICATION COMPLETE ===
will show and starts
then I add the script into crontab...I get this permission error
Failed to start nginx.service: Interactive authentication required.
Does anyone know how I can fix this?
Thanks in advance for any advices.