Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Re^2: Multiple commands with one system call

by renzosilv (Novice)
on Sep 29, 2011 at 15:28 UTC ( [id://928600]=note: print w/replies, xml ) Need Help??


in reply to Re: Multiple commands with one system call
in thread Multiple commands with one system call

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.

  • Comment on Re^2: Multiple commands with one system call

Replies are listed 'Best First'.
Re^3: Multiple commands with one system call
by TomDLux (Vicar) on Sep 29, 2011 at 16:07 UTC

    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.

Re^3: Multiple commands with one system call
by zentara (Archbishop) on Sep 29, 2011 at 16:59 UTC
Re^3: Multiple commands with one system call
by jethro (Monsignor) on Sep 30, 2011 at 09:31 UTC

    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://928600]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (5)
As of 2024-04-18 20:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found