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

Hi,

I am working in a Perl script in Window OS where the script should follow following process,
1: Start
2: launch some java process "java ......jar" in background
3: proceed further (.....some code......)
4: check if background process is still running... if not re-launch it.
5: proceed further (.....some code......)
6: check if background process is still running if yes kill it.
5: End
Can any one suggest me how is it possible to handle background process in this way, I am lost.
Thanks,
tart

Replies are listed 'Best First'.
Re: Background process
by BrowserUk (Patriarch) on Jan 11, 2011 at 05:32 UTC

    At its simplest all you need is:

    my $pid = system 1, q[java \path\to\yourapp.jar]; ... ## Kill it if it is still running kill 3, $pid if kill 0, $pid;

    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.

      Wowzers. I hadn't seen that before and finding the documentation for it took Google's help: perlport.

      As an optimization, may not call the command shell specified in $ENV{PERL5SHELL}. system(1, @args) spawns an external process and immediately returns its process designator, without waiting for it to terminate. Return value may be used subsequently in wait or waitpid. Failure to spawn() a subprocess is indicated by setting $? to "255 << 8". $? is set in a way compatible with Unix (i.e. the exitstatus of the subprocess is obtained by "$?>> 8", as described in the documentation). (Win32)

        Yes. It is very useful. I'm surprised that no one has thought to back port this to *nix.

        The only fly in the ointment is that it truncates the potentially 32-bit return value from the executable to 8-bits.


        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.
Re: Background process
by dasgar (Priest) on Jan 11, 2011 at 04:49 UTC

    You might want to look at Win32::Process. You can use it to launch your Java process and use the GetExitCode function to determine if the process is still running or not. To kill the process, use the Kill function or the KillProcess function.

    Unless I'm overlooking something, I think that should get you started down a path to achieve what you want to do.