SilverOne
Posts: 16
Joined: Sat Feb 02, 2013 12:11 am

restart processes on crash

Sat Feb 16, 2013 2:14 am

Hello !

I'm running several services such as openVPN/murmur/samba on my raspberry pi, i'd like to restart these services should any of them crash.

i made the following bash script but i don't know if it's the most efficient way to do it or if the script it self is any good. Anyway, you can check out the script here: http://paste.ubuntu.com/1660882/

I'd also like to know if there's any good way to "log" these crashes, perhaps i should make a log file on /var/log ??
Best Regards,
SilverOne

SirLagz
Posts: 1705
Joined: Mon Feb 20, 2012 8:53 am
Location: Perth, Australia
Contact: Website

Re: restart processes on crash

Sat Feb 16, 2013 7:55 am

I'd do it something like this

Code: Select all

#!/bin/bash
arrProc=( mumble-server openvpn smbd nmbd )
if [[ `pidof $x | wc -l` -eq 0 ]]; then
	echo "$x has crashed! Restarting...." >> /var/log/crashlog
	if [[ $x == "smbd" || $x == "nmbd"]]; then
		/etc/init.d/samba restart
	else
		/etc/init.d/$x start
	fi
fi
If you wanted to have a timestamp, then replace the echo line with this

Code: Select all

echo `date +%Y-%m-%d\ %H:%M:%S` "$x has crashed! Restarting..."
Run that every say 5 minutes with a cronjob and that will keep it going.
Last edited by SirLagz on Sat Feb 16, 2013 8:07 am, edited 1 time in total.
My Blog - http://www.sirlagz.net
Visit my blog for Tips, Tricks, Guides and More !
WiFi Issues ? Have a look at this post ! http://www.raspberrypi.org/phpBB3/viewtopic.php?f=28&t=44044

DBryant
Posts: 281
Joined: Sat Feb 02, 2013 12:41 pm
Location: Berkshire, UK

Re: restart processes on crash

Sat Feb 16, 2013 8:02 am

Well your script will do a job as long as it keeps running, who or what is monitoring it?

You can write log records using logger, a shell command interface to syslog.

See the man page at http://unixhelp.ed.ac.uk/CGI/man-cgi?logger+1.

And some more descriptive stuff at http://www.cyberciti.biz/tips/howto-lin ... yslog.html for example.

SilverOne
Posts: 16
Joined: Sat Feb 02, 2013 12:11 am

Re: restart processes on crash

Sat Feb 16, 2013 7:54 pm

Thank you both for your answers !
Sirlagz your code looks much cleaner than mine! i further customized it abit !

Code: Select all

#!/bin/bash
for i in murmurd openvpn ; do
if [[ $( pidof "$i" | wc -l ) -eq 0 ]] ; then
	echo $( date +%Y-%m-%d\ %H:%M:%S ) "$i has crashed! Process Restarted" >> /var/log/crashlog
	logger "$i" has crashed! Process Restarted. 
	/etc/init.d/"$i" start
fi
done

if [[ $( pidof smbd | wc -l ) -eq 0 || $( pidof nmbd | wc -l ) -eq 0 ]]; then
	/etc/init.d/samba restart
	echo $( date +%Y-%m-%d\ %H:%M:%S ) "Samba has crashed! Process Restarted"
        logger Samba has crashed! Process Restarted. 
fi
exit
DBryant thank you for the logger command, i was not aware of its existence. i left the echo on because i have DHCP on and the syslog is riddled with DHCP renewal messages. I should really turn DHCP off since i don't need it at all.

thank you again!
Best Regards,
SilverOne

SirLagz
Posts: 1705
Joined: Mon Feb 20, 2012 8:53 am
Location: Perth, Australia
Contact: Website

Re: restart processes on crash

Mon Feb 18, 2013 12:14 am

No problems, happy to have helped :)
My Blog - http://www.sirlagz.net
Visit my blog for Tips, Tricks, Guides and More !
WiFi Issues ? Have a look at this post ! http://www.raspberrypi.org/phpBB3/viewtopic.php?f=28&t=44044

Return to “Beginners”