5

I am trying to make a crontab to start nginx if it is stopped.

I googled and found these two scripts

http://www.akamaras.com/linux/linux-script-to-check-if-a-service-is-running-and-start-it-if-its-stopped/

#!/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

https://www.digitalocean.com/community/tutorials/how-to-use-a-simple-bash-script-to-restart-server-programs

#!/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 nginx

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.

3

Those scripts you have been trying to use are obsolete and shouldn't be used on a modern system with systemd.

Try a script like this instead:

#!/bin/bash
if ! systemctl is-active nginx >/dev/null ; then
    systemctl start nginx
fi

But, that is some terribly nasty hackery and probably not necessary, so before you do that, try having systemd restart nginx automatically if it stops. Do that with a systemd drop-in:

[Service]
Restart=always

which you place as the file /etc/systemd/system/nginx.service.d/override.conf (creating the directory if it doesn't exist). You can also use systemctl edit nginx to create the file.

And of course, either creating the systemd drop-in, or putting this script in crontab, must be done as root (try using sudo -i for a long running root shell).

7
  • thanks a lot for the info, do you know how long it'll check and restart? So I can keep watch on my dev as testing
    – Dora
    Apr 7 '18 at 18:52
  • @Dora systemd will notice and restart immediately, so you should definitely try that method first. Apr 7 '18 at 18:54
  • pretty much just in time for this, I added the restart method then I started up my nginx now. As I recall it's almost time it'll somehow stop within the next 45minutes.
    – Dora
    Apr 7 '18 at 18:56
  • as for the sudo -i so you mean I should use this command first then use crontab -e which sudo -i will override the permission as root?
    – Dora
    Apr 7 '18 at 18:57
  • @Dora It should be obvious when you run sudo -i. You will have a root shell and further commands do not need sudo until you exit that shell. Apr 7 '18 at 18:59
2

Don't do this via cron at all.

Nginx itself is very stable, it won't just stop the service for no reason. You can make it unstable with an application though. If the application is unstable, just try-restart it hourly-daily-weekly-whatever.

It's less outage than waiting about 30 seconds until cron kicks in. You can restart at night, whereas you can be assured any instabilities will hit you when high traffic happens.

Chances are that you will stop nginx for maintenance and you'd be suprised by cron starting it back in the middle of your work.

Think about pacemaker cluster if the environment is very unstable.

And work/instist on application bugs to be fixed, so it can run a month without a hang/death.

3
  • I totally get what you mean, this is actually a temp solution for at this moment while trying to figure out what is going on. What is weird now is that it will stop everyday approx between 0000 - 0030 and 1200 - 1230. And I need to manually start it. I checked logs and everything but can't find a thing at all. Still trying to figure out piece by piece but at this moment, this is a temp solution to give myself a break that I don't have to check it out every twelve hours
    – Dora
    Apr 7 '18 at 5:18
  • Ah, I see. This fits into "pacemaker cluster if the environment is very unstable". Even a one-node pacemaker has a service restart capability @Dora.
    – kubanczyk
    Apr 7 '18 at 8:11
  • I kinda googled, so I need to install something called pacemaker and Corosync? do you have a good tutorial url for this? As I am still quite new to this
    – Dora
    Apr 7 '18 at 16:36
-1

I agree with the comment that you should not do this by cron. However if you still insist, I think the problem happens as you do not get authorization by sudo -s command.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.