Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re: Running a process in the background

by terra incognita (Pilgrim)
on Nov 23, 2004 at 22:40 UTC ( [id://410008]=note: print w/replies, xml ) Need Help??


in reply to Running a process in the background

What OS are you running?

Under XP the start cmd will launch a separate cmd shell for each program you specify and then return to the calling cmd file.
Are you calling start from a Perl script?
Do you want the processes to return to the calling program (then you need threads).

Some code would be great, so that we can understand exactly what you are doing.

  • Comment on Re: Running a process in the background

Replies are listed 'Best First'.
Re^2: Running a process in the background
by Anonymous Monk on Nov 23, 2004 at 22:55 UTC
    I am running Windows XP. Start does launch a separate cmd shell as you have mentioned.

    I am calling start from a perl script:

    my $output = `start /b perl test1.pl > testing.log`; test2();

    So basically I want test2() to run when test1.pl is executed in the background. But what really is happenning is that test1.pl completes its own commands and then test2() is executed.

    How can I make them run simulataneously? Thanks.

      I am running Windows XP.
      Ah, that changes things considerably. You could use the standard Unix, on Windows emulated approach, such as fork. But you can use Windows specific methods, too, such as using Win32::Process. See They didn't give me a fork so I have to eat with a spawn..

      As a variaton on that theme, I made a little module that, when used in a program, restarts the script with the same parameters, but without a console:

      # Save as module file Win32/Detached.pm, somewhere in @INC package Win32::Detached; use strict; use Win32; use Win32::Process; use Cwd; if(@ARGV and $ARGV[0] eq '-nolaunch') { shift; } elsif(!$^C) { Win32::Process::Create( my $ProcessObj, $^X, join(" ", map { tr/ // ? qq["$_"] : $_ } $^X, $0, "-nolaunch", + @ARGV), 0, DETACHED_PROCESS, cwd, ) or die Win32::FormatMessage(Win32::GetLastError()); exit; } 1;

      Simply do

      use Win32::Detached;
      in your main script, just making sure you don't use "-nolaunch" as a command line switch.

      If that module is not handy for you — to be honest, I've rarely ever used it — you can at least borrow the parameters for the API call.

      If you want to permanently run a perl script in the background, check out how to run a perl script as a service — for more detailed info, see also this online article.

        Almost four years later, but still amazingly helpful. Thank you for this. :)

        Also, why is this not on cpan? ;)
        Hello bart,

        Thanks for the response. I would like to try out your module. How can I use this module to run test1.pl in the background?

        If you could provide an example of how to use the module, that would be greatly appreciated.

        Thanks.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://410008]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (4)
As of 2024-04-25 06:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found