Start services if not running

Very occasionally openvpn or the SMB service has stopped when I’m abroad. If it’s the SMB service I can use VPN to login to ipfire and start the SMB service. However, if it’s openvpn, I can’t use my server anymore to access files.

How can I make these (or any) services automatically restart if they stop? Or maybe even just attempt to start them every, say, 24 hours, assuming they won’t start if they’re already running?

Thanks

Praful

I think some people use “Watchdog”?
Network
Connection Scheduler

reboot every week

monit also has the ability to restart a stopped service. See the examples on this page:

2 Likes

Thanks Jon - monit looks perfect for my requirements.

Praful

Thanks for replying, Shaun. Good to know there’s an option to restart the server.

An other way is to use a cron script like this

ps -ef | grep fcron | grep -v grep
[ $? -eq “0” ] && echo “process is running” || /etc/rc.d/init.d/fcron start

to add a new file check-fcron in /etc/fcron.minutely/ for exemple

Thanks. Good to know of a simpler option like cron. Thanks for providing an example.

Hello,

This is an example for samba service :

First you can check service status :
/etc/rc.d/init.d/samba status

create a file : nano /etc/fcron.minutely/check-samba

Paste this 2 lines :

ps -ef | grep samba | grep -v grep
[ $? -eq “0” ] && echo “process is running” || /etc/rc.d/init.d/samba start

to save : Ctrl x and y

chmod 754 /etc/fcron.minutely/check-samba

Now, you can test the script by stopping samba service, check status

/etc/rc.d/init.d/samba stop
/etc/rc.d/init.d/samba status

Samba should be down

run this command to take effect the fcron task
/etc/rc.d/init.d/fcron restart

Wait a minute and test service status :

/etc/rc.d/init.d/samba status
1 Like

This is an example for samba service

Thank you for taking the time to provide a detailed example.

I don’t have a process called samba, which is fine since you probably just gave that as an example. However, when I run

/etc/rc.d/init.d/samba status

the following is returned:

nmbd is running with Process ID(s) 22975.
smbd is running with Process ID(s) 22990 22989 22982 21569.
winbindd is running with Process ID(s) 23154 22994 22993.

Grepping for these processes returns three processes for smbd, one for nmbd and three for winbindd.

Please could you update your example for this situation?

For OpenVPN, I can’t see a script to start it /etc/rc.d/init.d/. Looking at processes, returns:

nobody    5621     1  0 Feb27 ?        04:36:56 /usr/sbin/openvpn --config /var/ipfire/ovpn/server.conf
root      5662     1  0 Feb27 ?        00:00:09 /usr/bin/python3 /usr/sbin/openvpn-authenticator -d

Do I need to be looking for both these processes? I noticed that there is no pid file in /var/run for the authenticator. So I can configure just one in monit.

Thanks
Praful

1 Like

Praful,

I think this batch can check the status of each desired process :

ps -ef | pgrep smbd | grep -v grep
[ $? -eq "0" ] && echo "process is running" || /etc/rc.d/init.d/samba start

For OpenVPN, I don’t find the solution yet :frowning:

As you can see from the output, a feature with several processes has to be handled a bit more exactly.

Samba consists of smbd (file and printer sharing) , nmbd ( NetBIOS-to-IP-address name service ), winbindd ( Name service switch ). So the information ‘smbd is not running’ doesn’t imply ‘all Samba services are not running’.
Therefore it is better to do a /etc/rc.d/init.d/samba restart. This kills all associated processes before starting the service.

Thanks. If I understand correctly, I’d check for all three processes and if the process being checked is no longer running then I should restart samba (taking Bernhard Bitsch’s comment into account).

Thank you Bernhard !

For openVPN, I think have a solution. You should test this on your own installation before :

ps -ef | pgrep openvpn | grep -v grep
[ $? -eq "0" ] && echo "process is running" || /usr/local/bin/openvpnctrl -r

After validation, you could use this script in fcron.minutely :

ps -ef | pgrep nmbd | grep -v grep
[ $? -eq "0" ] && echo "process is running" || /etc/rc.d/init.d/samba restart

ps -ef | pgrep smbd | grep -v grep
[ $? -eq "0" ] && echo "process is running" || /etc/rc.d/init.d/samba restart

ps -ef | pgrep winbindd | grep -v grep
[ $? -eq "0" ] && echo "process is running" || /etc/rc.d/init.d/samba restart

ps -ef | pgrep openvpn | grep -v grep
[ $? -eq "0" ] && echo "process is running" || /usr/local/bin/openvpnctrl -r

/usr/local/bin/openvpnctrl -r (restart openvpn)
/usr/local/bin/openvpnctrl -k (stop or kill openvpn)
/usr/local/bin/openvpnctrl -s (start openvpn)

1 Like

That’s great Baptiste Bay! Many thanks for providing the streamlined solution.

I’ve learnt a few things from your posts today, which is useful because I switched to Linux Mint from Windows a few months ago on my laptop.