Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

Launching multiple programs from Perl script

by Shawrich (Initiate)
on Nov 06, 2012 at 15:41 UTC ( #1002506=perlquestion: print w/replies, xml ) Need Help??
Shawrich has asked for the wisdom of the Perl Monks concerning the following question:

I am working on a script which will launch several applications based upon user input to a Tkx form. In the past I have performed similar tasks using system or exec commands. However, system launches the first application and then waits for it to return, and exec launches the first application and then, of course, dies.

My idea was to have the perl code create a batch file to run all the applications, then just run the bat file with exec. Unless any of the monks around here have a better suggestion?

  • Comment on Launching multiple programs from Perl script

Replies are listed 'Best First'.
Re: Launching multiple programs from Perl script
by tobyink (Abbot) on Nov 06, 2012 at 15:49 UTC
    exec($cmd1) if fork; exec($cmd2) if fork; exec($cmd3) if fork;

    Also FYI, exec doesn't cause your script to "die"; it removes your script from the operating system's process table, replacing it with the executed command. Thus your script "becomes" the executed program.

    perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'
      Just as short but makes for a more intuitive process relationship---all $cmdX processes are children of the script instead of $cmd3 being a child of $cmd2 which is a child of $cmd1:
      fork or exec $cmd1; fork or exec $cmd2; fork or exec $cmd3;
      I tested this but found the code stopped after the first exec statement. Here is the portion of my code I'm working with.
      foreach ( keys %bridges ) { if ( $bridges{$_} ) { my $cmdStr = "\"C:\\Some Path\\$_\\Some File.exe\""; exec($cmdStr) if fork; } }
        You mean the exec never returned? Strange. fork/exec emulation is not exactly straightforward on Windows but it shouldn't be that broken. Works fine here on Linux. Are you sure it's not how %bridges is set up?
Re: Launching multiple programs from Perl script
by Anonymous Monk on Nov 06, 2012 at 15:55 UTC

      Proc::Background does not appear to like spaces in the file name. However, I found another post on this and it resolved my issue with the spaces. Re: Proc::Background does not take file path with spaces

      And here's the final, working code example for anyone else who comes along.
      foreach ( keys %bridges ) { if ( $bridges{$_} ) { my $cmdStr = Win32::GetShortPathName("C:\\Some Path\\$_\\Some +File.exe"); my $proc1 = Proc::Background->new($cmdStr); } }

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1002506]
Approved by marto
and the fire pops...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (4)
As of 2017-04-25 03:25 GMT
Find Nodes?
    Voting Booth?
    I'm a fool:

    Results (448 votes). Check out past polls.