Script to check a service is running and if not start it

I'm looking for a sh script (that I will run in cron) that checks if openvpn client is up, and if not start the service.

I'm new in scripting so please bear with me a little..

So my questions are;

  1. I don't now what to put in the script if the check is ok, so nothing has te be done.
  2. ps | grep -v grep | grep $service | wc -l gives 0 as output, but when openvpn is running and when it's not. So not the correct code :wink:
#!/bin/sh

service=/etc/init.d/openvpn

# Is openvpn running? If not start openvpn

if (( $(ps | grep -v grep | grep $service | wc -l) > 0 ))
	then
		?????                   # code if already running 
	else
		$service start 	 		# code if not started
fi


1 Like
cat << "EOF" > /etc/openvpn/keepalive.sh
if ! pgrep openvpn
then /etc/init.d/openvpn restart
fi
EOF
cat << "EOF" >> /etc/crontabs/root
* * * * * . /etc/openvpn/keepalive.sh
EOF
uci set system.@system[0].cronloglevel="9"
uci commit system
/etc/init.d/cron restart
3 Likes

Thank you, works like a charm.

1 Like

One question, I use crontab for more than one job, but what this one do exactly?

* * * * * .

It works, so that's not the problem, I just want to understand :wink:

# Example of job definition:
# .---------------- minute (0 - 59)
# |  .------------- hour (0 - 23)
# |  |  .---------- day of month (1 - 31)
# |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...
# |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# |  |  |  |  |
# *  *  *  *  *  user command to be executed

Every minute, every hour, every day, every month, every day of week... then . ... for command

1 Like

Is starting script line missing from keepalive.sh ?

#!/bin/sh

I know that one @erdoukki, but the command in crontab is really * * * * *
So no asterix is changed.

Everthing is working, that is not the issue here.

I do not understand, if you want more crontab lines just use another line to add a command...

Look at the script I've got from @vgaetera His script is working perfectly, as my vpn goes down, it restarts. So far so good.

I just want to understand wat the crontab line * * * * * means, because it's working.
It doesn't say check every minute 1 * * * * or every day at 12PM * 12 * * *

This one is for every minute 1...
Like 00h01, 01h01, 02h01...

I know, I think you don't understand my question . The real cronjob line is

* * * * * . /etc.openvpn/keepalive.sh

This works, because if my vpn goes down, it goes up almost instant. There isn't set a minute/hour/day/month/day of the week. I want to understand why.

* * * * * means every minute

It is the same like */1 * * * *

If you want the script to be executed at every 5 minute, it should be */5 * * * *

2 Likes

Oh ok, i didn’t know that * * * * * equals */1 * * * *
Thanks

1 Like

I think, it should work without dot.

Shebang and the executable bit are ignored if the script is invoked with the dot/source command.

2 Likes

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.