fork/system/child processes

Iain Buchanan iain at pcorp.com.au
Wed Dec 3 06:53:15 UTC 2003


On Wed, 2003-12-03 at 14:53, Don wrote:
> This isn't fedora-specific...
> 
> I have an application that MUST run forever.... if it crashes, dies,
> ends for any reason I want it to start again.

Ok,
1. assume (language == perl :)
2. This won't make sure your program runs forever, because the parent
could be killed
3. If you're not worried about the parent being killed, then why don't
you just use bash? or a simple loop? eg

(bash)
$ while :; do ./my-daemon.pl && break; echo "oops, it died"; sleep 1; done

(perl loop)
while (1) {
   `my-daemon.pl`;
   print "oops, it died\n";
}

They'll all die with a ctrl-c (make sure my-daemon.pl exits 1 on a sig
term, 0 on everything else)

[snip]
> 
> I tried:
> for (;1==1:)
>    $newpid = fork;
>    if ($newpid) {
> 	# parent...
> 	wait;
> 	#log unexpected end of command-to-run
>    } else {
> 	# child ...
>         system(command-to-run);
> 	exit;
>    }
> }

You should use fork with exec, not system.  Then if it behaves anything
like the C-style exec, just kill the child pid from the parent, to kill
the process you just exec'd.

HTH,
-- 
Iain Buchanan <iain at pcorp.nospam.com.au>

Assembly language experience is [important] for the maturity
and understanding of how computers work that it provides.
		-- D. Gries
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 189 bytes
Desc: This is a digitally signed message part
URL: <http://listman.redhat.com/archives/fedora-list/attachments/20031203/5943d749/attachment-0001.sig>


More information about the fedora-list mailing list