Operating System - HP-UX
1834295 Members
2365 Online
110066 Solutions
New Discussion

getting init to respawn indefinetly

 
James Myers
Occasional Contributor

getting init to respawn indefinetly

I have an application program running as a "daemon". I need a pool of these, it is intended to do a task and exit, and have another start to take its place. init would be ideal for starting and managing these processes, but it thinks they respawn too quickly, and gives up on restarting them (10/2minutes, according to the man page).

Is there a way to avoid the default behavior of init for my entries in inittab so that they can respawn quicker that 10 times per 2 minutes. I imagine this would even be a problem for something like getty that respawns from init, if you had a modem program that fast.
6 REPLIES 6
Todd McDaniel_1
Honored Contributor

Re: getting init to respawn indefinetly

sounds like you need a wrapper program instead of the inittab as your method. have a script restart the application after it completes...

You can use this format for a down and dirty

proc=`ps -aef |grep "proc" |grep -v grep |wc -l`
while [$proc -ne 1 ]
do
...
...
done

Just off the top of my head, but should work.

Unix, the other white meat.
Bill Hassell
Honored Contributor

Re: getting init to respawn indefinetly

init only works like tha man page documents: more than 10 times in 2 minutes and the error message is generated followed by a 5 minute pause in respawning that program. This type of process is much more suited to scripting. Start the script as a background process and in the script, just do something like this:

while :
do
/somedir/someprogram some_prameters..
sleep 1
done

That will re-run the program forever with a one second pause between each run. Just put this script name into a start/stop script in /sbin/init.d and link it into the /sbin/rc* directories.


Bill Hassell, sysadmin
Kent Ostby
Honored Contributor

Re: getting init to respawn indefinetly

The other thing you can do is write "script1" as follows:

#!/bin/sh


exit

So it will call itself right before it ends.

Best regards,

Kent M. Ostby
"Well, actually, she is a rocket scientist" -- Steve Martin in "Roxanne"
Dave Johnson_1
Super Advisor

Re: getting init to respawn indefinetly

Or better yet

#!/bin/sh

exec <script1>

The exec will run the "new" command in the current shell with out creating a new process.

-Dave
Jordan Bean
Honored Contributor

Re: getting init to respawn indefinetly

You need a "pool" of them? Do you mean that you need several concurrently running instances of the same application?

I wouldn't run all of them from init directly. Instead, use a script similar to that attached to keep a limited number of daemons running at all times.

I haven't thoroughly tested this, so be careful! You may end up filling up the process table.

Elmar P. Kolkman
Honored Contributor

Re: getting init to respawn indefinetly

I haven't tested it, but what you could do to solve your problem is use a script to start the daemon and to make sure the script always runs, put that in the inittab... What way init only has to respawn your script, not the daemon itself. And that script will run the way it should.

Script will look something like this:
#! /sbin/sh
PATH=/sbin:/bin # Add anything the daemon needs
export PATH
while true
do


sleep 1
done

Also, make sure the daemon doesn't fork-and-exit like normal daemons do... That might be the cause of your problem too, of course. You can check it by doing at the testing line:
if [ $(ps -ef | grep | grep -v grep | wc -l) -gt 0 ]
then
echo Daemon exited, but is still running
exit
fi

Of course, check the script by just running it from command line.
Every problem has at least one solution. Only some solutions are harder to find.