newrisedesigns has asked for the wisdom of the Perl Monks concerning the following question:

I have a script that I would like to have automatically restart when a die() occurs.

When I use the following code, if spits out some junk (represented as some unicode characters) when it dies. It only does this the first time. Every observed restart/die is does so cleanly without those characters.

I'm concerned that this "junk" might cause my looping program (a Net::FTP download script) to end and not restart. Do you see this as a concern, and if so, any clue why it's dumping garbage the first time around?

#!/usr/bin/perl -w $SIG{__DIE__} = \&restart; print "Dying!\n"; sleep(5); die($!); sub restart{ print "Restart...\n\n"; exec($0); } __END__ D:\projects\test>dietest.pl Dying! Restart... ??D:\projects\test>Dying! Restart... Dying! Restart... Dying!

Now that I think of it... is this the best way to restart a dying program? Is there some cleaner/more reliable method?

Thanks in advance,

John J Reiser
newrisedesigns.com

Replies are listed 'Best First'.
Re: Problem with using $SIG{__DIE__} to restart program
by bluto (Curate) on Oct 24, 2002 at 17:06 UTC
    It's usually better to have a different process act as a "nanny" and just restart your program if it dies. Once nice thing about this is that you can make decisions about restarting (e.g. by checking the exit code, or not restarting if you've already done that N times in M minutes).

    There are various ways of doing this on Unix, but I'm not familiar with Windows. If you have a reliable fork(), one way is to just fork your program during startup and have the parent wait() or waitpid() for the child to die and respawn as need.

    bluto

Re: Problem with using $SIG{__DIE__} to restart program
by fglock (Vicar) on Oct 24, 2002 at 17:19 UTC

    have a different process act as a "nanny" and just restart your program if it dies

    Another reason is that "die" won't always work: a "killed" program doesn't receive a "die" signal - it just dies.