NAME
Proc::Simple -- launch and control background processes
SYNOPSIS
use Proc::Simple;
$myproc = Proc::Simple->new(); # Create a new process object
$myproc->start("shell-command-line"); # Launch a shell process
$myproc->start(sub { ... }); # Launch a perl subroutine
$myproc->start(\&subroutine); # Launch a perl subroutine
$running = $myproc->poll(); # Poll Running Process
$proc->kill_on_destroy(1); # Set kill on destroy
$proc->signal_on_destroy("KILL"); # Specify signal to be sent
# on destroy
$myproc->kill(); # Kill Process (SIGTERM)
$myproc->kill("SIGUSR1"); # Send specified signal
Proc::Simple::debug($level); # Turn debug on
DESCRIPTION
The Proc::Simple package provides objects that model real-life processes from a user's point of view. A new process object is created by
$myproc = Proc::Simple->new();
Either shell-like command lines or references to perl subroutines can be specified for launching a process in background. A 10-second sleep process, for example, can be started via the shell as
$myproc->start("sleep 10");
or, as a perl subroutine, with
$myproc->start(sub { sleep(10); });
The start Method returns immediately after starting the specified process in background, i.e. non-blocking mode. It returns 1 if the process has been launched sucessfully and 0 if not.
The poll method checks if the process is still running
$running = $myproc->poll();
and returns 1 if it is, 0 if it's not. Finally,
$myproc->kill();
terminates the process by sending it the SIGTERM signal. As an option, another signal can be specified.
$myproc->kill("SIGUSR1");
sends the SIGUSR1 signal to the running process. kill returns 1 if it succeeds in sending the signal, 0 if it doesn't.
The methods are discussed in more detail in the next section.
A destructor is provided so that the forked processes can be sent a signal automatically should the perl object be destroyed or if the perl process exits. By default this behaviour is turned off (see the kill_on_destroy and signal_on_destroy methods).
METHODS
The following methods are available:
- new (Constructor)
-
Create a new instance of this class by writing
$proc = new Proc::Simple;
or
$proc = Proc::Simple->new();
It takes no arguments.
- start
-
Launch a new process. For an external program to be started like from the shell, call
$status = $proc->start("program-name");
If, on the other hand, you want to start execution of a Perl function in the background, simply provide the function reference like
$status = $proc->start(\&perl_function);
or supply an unnamed subroutine:
$status = $proc->start( sub { sleep(1) } );
The start Method returns immediately after starting the specified process in background, i.e. non-blocking mode. It returns 1 if the process has been launched sucessfully and 0 if not.
- poll
-
The poll method checks if the process is still running
$running = $myproc->poll();
and returns 1 if it is, 0 if it's not.
- kill
-
The kill() method:
$myproc->kill();
terminates the process by sending it the SIGTERM signal. As an option, another signal can be specified.
$myproc->kill("SIGUSR1");
sends the SIGUSR1 signal to the running process. kill returns 1 if it succeeds in sending the signal, 0 if it doesn't.
- kill_on_destroy
-
Set a flag to determine whether the process attached to this object should be killed when the object is destroyed. By default, this flag is set to false. The current value is returned.
$current = $proc->kill_on_destroy; $proc->kill_on_destroy(1); # Set flag to true $proc->kill_on_destroy(0); # Set flag to false
- signal_on_destroy
-
Method to set the signal that will be sent to the process when the object is destroyed (Assuming kill_on_destroy is true). Returns the current setting.
$current = $proc->signal_on_destroy; $proc->signal_on_destroy("KILL");
- pid
-
Returns the pid of the forked process associated with this object
$pid = $proc->pid;
- DESTROY (Destructor)
-
Object destructor. This method is called when the object is destroyed (eg with "undef" or on exiting perl). If kill_on_destroy is true the process associated with the object is sent the signal_on_destroy signal (SIGTERM if undefined).
- debug
-
Switches debug messages on and off -- Proc::Simple::debug(1) switches them on, Proc::Simple::debug(0) keeps Proc::Simple quiet.
NOTE
Please keep in mind that there is no guarantee that the SIGTERM signal really terminates a process. Processes can have signal handlers defined that avoid the shutdown. If in doubt, whether a process still exists, check it repeatedly with the poll routine after sending the signal.
Requirements
I'd recommend using at least perl 5.003 -- if you don't have it, this is the time to upgrade! Get 5.005_02 or better.
AUTHORS
Michael Schilli <michael@perlmeister.com>
Tim Jenness <t.jenness@jach.hawaii.edu> did kill_on_destroy/signal_on_destroy/pid
1 POD Error
The following errors were encountered while parsing the POD:
- Around line 453:
You forgot a '=back' before '=head1'