Dave's Brain

Browse - Computer Tips - How can I restart a service/daemon when it dies?

Date: 2010jun1
OS: Linux
Language: bash
Warning: This doesn't work with systemd

Q.  How can I restart a service/daemon when it dies?

A.  Of course, its best to locate the reason why its dying.  But
as a Bandaid you can do this.

If your service MYSERVICE is at issue make file /etc/cron.hourly/check_MYSERVICE that contains: 

#!/bin/sh if /sbin/service MYSERVICE status | grep -q 'is running'; then : else echo "MYSERVICE seems to be down -- restarting" /sbin/service MYSERVICE restart fi
And make it executable:
chmod a+x /etc/cron.hourly/check_MYSERVICE
You'll get mail when it restarts the service (no mail when checks it and its OK). You can do this for multiple services.

Add a comment

Sign in to add a comment
Copyright © 2008-2017, dave - Code samples on Dave's Brain is licensed under the Creative Commons Attribution 2.5 License. However other material, including English text has all rights reserved.