views:

539

answers:

8

Hi,

I developed a software (in C++) which needs to be continuously running. That basically means that it has to be restarted each time it stops.

I was thinking about using cron jobs to check every minutes if it is still alive, but there might be a cleaner way or standard way of doing this.

Thanks in advance

A: 

This is not so easy. If you're thinking "I know, I'll write a program to watch for my program, or see if such a program already exists as a standard service!" then what if someone kills that program? Who watches the watcher?

unwind
Yeah, I know, this is why I ask the question :-)I secretely hoped that Linux had a feature for this...
Barth
The first program, so they watch each other. Yes, this still can b defeated, but it's tricky..
Paul
If you have 'that program' running under certain priv you won't need to worry about someone killing it. and if root kills it then they probably needed to.
PintSizedCat
You have obviously never heard of Friar Tuck and Robin Hood (http://www.textfiles.com/virus/robin.hod).
Chas. Owens
@Chas. Owens ++ awesome story.
PintSizedCat
+6  A: 

I believe the easiest way to do this is to have a script that will start your program and if it gets returned to it just restarts it.

#!/bin/sh
while true; do
  ./Your_program
done
PintSizedCat
and who watches the watcher?
Alnitak
I've used a more complex version of this trick (I have a perl script that starts a set of daemons while paying attention to startup dependency relationships). In practice a simple enough script (like the example given) will not fail unless the system is in big trouble . It's not foolproof, but it's simple and unlikely to fail unless the system is collapsing around you anyway.
Michael Kohne
I voted this answer down because while it may solve the problem for a developer, it's a typical case where the solution should be provided by a knowledgeable system administrator. What happens if your program dies intermediately? Not uncommon if a needed file has been deleted by example. This loop will kill the performance of the machine and risk even to bring it down. This is a sysadmin's never-ending-loop. DO NOT USE THIS SOLUTION!
A: 

Create the program you wish to have running continually as a child of a "watcher" process that re-starts it when it terminates. You can use wait/waitpid (or SIGCHILD) to tell when the child terminates. I would expect someone's written code to do this (it's pretty much what init(8) does)

However, the program presumably does something. You might want not only to check the application is running, but that it is not hung or something and is providing the service that it is intended to. This might mean running some sort of probe or synthetic transaction to check it's operating correctly.

EDIT: You may be able to get init to do this for you - give it a type of 'respawn' in inittab. From the man page:

respawn
    The process will be restarted whenever it terminates (e.g. getty).
Paul
+7  A: 

Fedora and Ubuntu use upstart, which has a the ability to automatically restart your deamon if it exits.

karunski
+3  A: 

Monit can do what you want and much more.

cron is an option if your app will be smart enough to check for itself running (this is to avoid many copies of it running). This is usually done in a standard way via PID files.

Anonymous
If the application takes too many resources to start running it will be too heavy handed. The cron method is useful for lightweight apps.
Baruch Even
+2  A: 

launchtool is a program I used for this purpose, it will monitor your process and restart it as needed, it can also wait a few seconds before reinvocation. This can be useful in case there are sockets that need to be released before the app can start again. It was very useful for my purposes.

Baruch Even
Does it work with Red Hat ?
Barth
It looks like launchtool is not packaged for Fedora, so I doubt it will be in RedHat.
Baruch Even
+4  A: 

There are two proper ways to do it on *nix:

  1. Use the OS infrastructure (like smf/svc on solaris, upstart on Ubuntu, etc...). This is the proper way as you can stop/restart/enable/disable/reconfigure at any time.

  2. Use "respawn" in /etc/inittab (enabled at boot time).

respawn in inittab also solves the "who watches the watcher". If init dies (or is killed) the system will panic so you dont need to worry about restarting the process.
camh
That's the task of the infrastructure. You have *one* watcher (that will be restarted if necessary) for hundreds of processes. You don't want to panic and crash the machine for one process.
A: 

How about a script that check about every 10 minutes to see if the application is running and if it isn't it will restart the computer. If the application is running, then it just continues to check.

Here is my script using PrcView is a freeware process viewer utility. And I used notepad.exe as my example application that needs to be running, I am not sure the command for checking every 10 minutes and where it would go in my script.

@echo off PATH=%PATH%;%PROGRAMFILES%\PV;%PROGRAMFILES%\Notepad PV.EXE notepad.exe >nul if ERRORLEVEL 1 goto Process_NotFound :Process_Found echo Notepad is running goto END :Process_NotFound echo Notepad is not running shutdown /r /t 50 goto END :END

DesertDawg