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

I've got a smallish app on Win32. Everything works now (thanks Monks!) except for one small detail: I need the script to terminate itself at the "end". My script currently fork()'s a number of processes, so I'm guessing it's waiting for the return code(s). This isn't going to happen, as the programs I forked will be running indefinitely - but I still need the script (and it's associated console window) to go away. Once the script executes, I can currently hit cntrl-c, the script will stop, and all my fork()'ed programs continue running. This is what I want them to do, without me having to hit ctrl-C.

Suggestions (other than "that's horribly inelegant, don't do that" ;))??

Thanks,

Glenn

Replies are listed 'Best First'.
Re: forcibly exiting script
by tachyon (Chancellor) on Aug 14, 2001 at 04:40 UTC

    I was just mucking around with Win32::Console. You can also use this to fork a detached process just fine. Here is a little alarm clock daemon example. When it runs it pops a console and asks for a sleep time. It then detaches from the console which closes. Next it forks. The parent just exits whilst the child sleeps for x seconds, pops a new console window and prints a message (just to prove it is alive and kicking). This new console disappears when the child exits 5 seconds later.

    #!/usr/bin/perl -w use strict; use Win32::Console; my $con = Win32::Console->new(); $con->Display; $con->Write("Sleep how many seconds? "); chomp(my $sleep = <STDIN>); $con->Free(); # detatch our script from the console which closes # now let's fork of an alarm clock child defined ( my $pid = fork() ) or die "Can't fork $!\n"; if ($pid) { exit; } else { sleep $sleep; $con->Alloc() or die $!; $con->Write("BZZT - this is your wake up call"); sleep 5; exit; }

    cheers

    tachyon

    s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

      However, in my case, I want the script to send output to the console, read some input, fork a process, send more output to the (same) console, fork another process, send more output, fork another process, send more output, then quit. As I understand $con->Free(), I think it permanently detaches from the console. Sorry for being unclear.

      Thanks,

      Glenn

        OK, using the Win32::Process with DETACHED_PROCESS when starting the app seems to have cleared up my problem. Honestly, my code in total looks like garbage, but it runs. Thank you Perl ;)

        Later,

        Glenn

Re: forcibly exiting script
by tachyon (Chancellor) on Aug 14, 2001 at 03:00 UTC

    What you essentially want to do is create a detached process/daemon. Use Super Search (words in text) on this string "win32 detached process" and you will find heaps of links that show how you do this.

    cheers

    tachyon

    s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

Re: forcibly exiting script
by IraTarball (Monk) on Aug 14, 2001 at 02:30 UTC
    I don't think that you can do this. If you take a look at perldoc perlfork it tells you that forking is emulated on Win32 so as far as the OS is concerned all children reside in the same process space. There's also a paragraph in there that says, well, here it is...

    A way to mark a pseudo-processes as running detached from their parent (so that the parent would not have to wait() for them if it doesn't want to) will be provided in future.

    Bummer.

    update A little knowledge can make you abulic.
    I just followed tachyon's suggestion and see that there are in fact ways to do this.

    Good Luck,
    Ira,

    "So... What do all these little arrows mean?"
    ~unknown