Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: Multiple commands with one system call

by jethro (Monsignor)
on Sep 29, 2011 at 15:09 UTC ( [id://928597]=note: print w/replies, xml ) Need Help??


in reply to Multiple commands with one system call

One simple way to start more than one program simultaneously (on linux) is to use the shell to execute them in the background. I.e.

system("program1 &"); system("program2 &");

would run both programs concurrently, the last one wouldn't even need to run in the background. Programs in the background should not do any screen IO because they are detached from the terminal screen. But you can redirect output to files or /dev/null if that output is uniteresting

Another possibility is to use open with a pipe, i.e. open($f,"-|","program1"); which would enable you to get further input over STDIN to the program.

A third possibility is to use fork to do the (implicit) forking done by the previous methods yourself. And probably the very best possibility is to let a module like Parallel::ForkManager handle most of that work.

Replies are listed 'Best First'.
Re^2: Multiple commands with one system call
by renzosilv (Novice) on Sep 29, 2011 at 15:28 UTC

    Hey!

    I had tried using the system call with the "&" sign but the problem with that is that I also need to know when this program is closed by the user so I can run clean up commands. When I make a regular system call I can just run my clean up commands after because they won't be run until after the program is closed.

    Is there an easy way for me to catch when this program is being closed ?

    Thanks again for all your help.

      It's worth considering whether you need this degree of control, or whether it would be enough to do something simpler...say, run a job once an hour automatically.

      If you need this degree of control and interactivity, I would have a wrapper script which launches your program, storing the process id (PID ) of the process. Then at regular intervals it can verify the process is still running, using ps. When the process stops, the wrapper can send a message.

      A simple way to indicate the status is to create an indicator file, and delete it at the end. You can 'touch' the file at intervals to demonstrate the process is running and not crashed. Alternate, using the open with a pipe strategy, you can send text messages at intervals, 'still running', 'crashed' or 'completed'.

      As Occam said: Entia non sunt multiplicanda praeter necessitatem.

      Sure, lots of ways. For example instead of calling your program you could call a wrapper script that calls your program and on exit creates a file/appends to a file/touches a file. This can be detected by your script

      #!/bin/sh # wrapper script program1 touch /tmp/finishedwith$1

      You could call this from perl with a random number as parameter (the $1) so that each finish would create a different file. In the perl script you would monitor for the existance of this file

      This is easy but not very clean and safe. Another way is to use fork instead of system, because fork tells you the process id of the newly created process. And you can wait() for the process to finish (which would block your script) or check if the process is still running (somehow ;-).

      But really, look at Parallel::ForkManager, all the functions you want are already there. With "run_on_finish" you can choose a subroutine that is called when the process finishes.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (4)
As of 2024-04-20 00:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found