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?
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With