in reply to Using Proc::Background and Win32

It's not paramount that I use the Proc::Background Module, but I do need to run the command in
in the background and have the ability to check on if it's still running or not...

So I tried this way which seemed to execute my script in the background without ANY problems at
all. But when I attempt to check on it's progress I get the same result no matter if it's running
or not.

my $pid = system 1, "bg_script.pl $ARG1 \"$ARG2\" $ARG3 $ARG4 $ARG5"; print "\$pid == '$pid'\n"; my $still_running = kill 0, $pid; print "BEFORE SLEEP: \$still_running == '$still_running'\n\n"; sleep 10; $still_running = kill 0, $pid; print "AFTER SLEEP: \$still_running == '$still_running'\n\n";

The variable $still_running returns "1" on both executions...

Am I not doing the "kill" command correctly? According to Perldoc that is in the correct format, which is:
http://perldoc.perl.org/5.14.2/functions/kill.html

kill SIGNAL, LIST

Where LIST is the processes to check and SIGNAL, when zero, does nothing but check if it's alive still. But
when I run the check:
$still_running = kill 0, $pid;
It returns 1 no matter if it's running or not, I can see that the bg_script.pl is done running after it does
the 10 second sleep. So I'm not sure why it's returning "1" both times?

If I can figure this one out then I can stop killing myself over the Proc::Background Command...


I was also looking at the Win32::Process page, but I saw that on the Proc::Background page it
actually uses the Win32::Process if you PC is Windows. So I guess it wouldn't help to implicitly use that
Module? Does that make sense?
http://search.cpan.org/~bzajac/Proc-Background-1.10/lib/Proc/Background.pm
http://search.cpan.org/~jdb/Win32-Process-0.14/Process.pm


Thanks Again for the replies fellas, much appreciated!

Thanks,
Matt

Replies are listed 'Best First'.
Re^2: Using Proc::Background and Win32
by BrowserUk (Patriarch) on Oct 18, 2013 at 19:55 UTC

    Try it this way;

    Perl> $pid = system 1, 'perl', '-E"sleep 20; say q[done]"';; Perl> print kill 0, $pid while sleep 1;; 1 1 1 1 1 1 1 1 1 1 1 1 done 1 0 0 0 0 0 0 0

    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
      Hey BrowserUK, thanks again for the reply!

      Humm.... I was going to do your example but your first line confused me a bit... Sorry, I'm not brand new to
      Perl or anything like that but I'm definitely no expert.

      But since my script uses Gtk2, I can't really use the "Sleep" command in the final 'production' script, since
      the regular 'sleep' command will pause the processing of Gtk events.

      But for pure testing purposes only I tried this and it seemed to work:
      my $pid = system 1, ".\\bg_script.pl $ARG1 \"$ARG2\" $ARG3 $ARG4"; print "\$pid == '$pid'\n"; while (kill 0, $pid) { print kill 0, $pid; print "\n"; sleep 1; } print "\n\nOUT OF WHILE LOOP\n\n"; ______________OUTPUT______________ C:\Script_dir>main_script.pl $pid == '3144' 1 1 1 1 1 1 0 OUT OF WHILE LOOP

      Nice, so it looks like its working that way.... Weird, do you think I had a typo the first time I posted
      that, or do you think I can't set it to a variable and check it that way?

      And just so you know, in my final script that was working before, with Proc::Background, which was before
      I attempted using the CavaPackager to turn this into a standalone executable. I was using a while loop and the
      usleep command from --> "use Time::HiRes qw( usleep );" which sleeps so quickly that I was able to continue
      processing Gtk commands during each loop. I can show the code for that if your interested...


      But anyway, in terms of the Win32::Process, when reading the cpan page for Proc::Background, it sounded like
      that Proc::Background Module when run on Win32 it automatically uses the Win32::Process Module.
      Is that right, or to try that Win32::Process, do I need to specifically call that, because it looks
      like there is quite a good bit of a difference between the two?


      Thanks Again,
      Matt

        But since my script uses Gtk2, I can't really use the "Sleep" command in the final 'production' script, since the regular 'sleep' command will pause the processing of Gtk events.

        I've never used gtk2, but it presumably has repeating timers like Tk. So, set up a repeating timer that does the kill.

        Nice, so it looks like its working that way.... Weird, do you think I had a typo the first time I posted that, or do you think I can't set it to a variable and check it that way?

        Dunno about a typo; but I strongly recommend you use 'perl' as the second arg to system.:

        my $pid = system 1, 'perl', ".\\bg_script.pl $ARG1 \"$ARG2\" $ARG3 $AR +G4";

        Without that, perl starts a shell, and the shell invokes perl; but the pid you get back is that of the shell not perl, so it doesn't tell you when the perl process ends.

        I attempted using the CavaPackager t

        Sorry. I have no knowledge of that at all.


        With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.