Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Linux software watchdog?

Can anybody tell me how to handle the software watchdog in Linux?

I have a program SampleApplication which runs continuously and I need to restart it if its hangs or closes unexpectedly.

I was Googling about this and found Linux has watchdog at /dev/watchdog but don’t know how to use it. Could someone help me with an example?

My question is where to I specify my application name and delay interval to restart?

like image 837
Verve Innovation Avatar asked Jan 31 '12 00:01

Verve Innovation


4 Answers

The linux software watchdog will reboot the machine, not just restart your process.

Well this is simply not true, it is very possible to restart single or multiple processes after the watchdog signals that the systems is hanging - you can even ABORT the reboot or do a SOFT-reboot, one is able to configure "test" and "repair"-scripts / binaries which do whatever you want them to do. The busybox-version of watchdog is stripped down to a near-unusable level ... i guess the world will never know why the busybox-devs decided to abandon primary functionalities - for now, it would be best to avoid busybox at all --> the speed-improvements are nearly inexistent, the size-decrease does not compensate the huge loss of functionality. /bin/bash is rather small - recompile everything with the flag "-Os" if size matters and you're good to go - an out-of-the-box watchdog which allows for just about everything one could want.

Oh and PLEASE do NOT create your own watchdog - that'll most likely leave you with unhandled errors and make your life bad one day.

like image 182
nope Avatar answered Sep 20 '22 15:09

nope


Most of the Unix/Linux init programs will manage daemons for you and restart them. Look into placing your service in /etc/inittab. Or you might be using Upstart or systemd.

All of these programs run as PID 1 and it is their job to monitor and restart system processes.

From your Busybox tag I would assume you are running an embedded system. On those, the System V style init scripts with all of their shell scripts are really overkill. You should probably rip all that out and replace it with entries in /etc/inittab or upstart or systemd jobs.

like image 39
Zan Lynx Avatar answered Sep 18 '22 15:09

Zan Lynx


How about using cron? Set up a small cron job that runs every minute. Check if your application is up (using ps) and if not, restart it.

Make a tiny script like this:

#!/bin/bash
if [ ! "$(pidof myapp)" ] 
then
  /path/to/myapp &
fi

You test if "myapp" is in the process list. "!" reverses the test. If it's not there, it runs "myapp". "&" is just so it starts in the background.

Add this to cron. Depending on your system and preferences there's several ways to do it. The classical one is to use crontab. There's lots of documentation on how to specify your crontab line, but you probably want something like this:

* * * * * /path/to/the/script.sh > /dev/null

This will run your test every minute of every hour of every… You get the idea.

like image 30
Janne Avatar answered Sep 17 '22 15:09

Janne


Use /etc/inittab you can utilize it to start in the specific run levels and if it is killed it shall be restarted automatically

n:2345:respawn:/path/to/app

This will make it respawn in run levels 2345 you probably only need 3 and 5 but this will work fine and is built into Linux.

like image 28
darren102 Avatar answered Sep 20 '22 15:09

darren102