Change working directory when using Proc::Background
I am using Proc::Background
to execute a user defined program from a Tk GUI so that the GUI doesn't freeze due to a system
call. I want to be able to start the user defined program from directories that are different from the location of this Perl script.
Is this possible? Is there a different module I should be using? I want to
- Run the process asynchronously.
- Be able to tell if the process is still running.
- Be able to kill the process.
- Have support for both Unix/Linux and Win32.
- Be able to specify the current working directory of the process.
Proc::Background
fulfills all those requests except for #5, at least as far as I can tell.
Edit: April 22, 2011
I tried Forks::Super, per a suggestion, but I couldn't get things working correctly. For example, I want to watch for a signal to kill/interrupt a process. I use something like:my $pid = fork { dir => $my_dir, cmd => [ $my_cmd, $my_args ] };
my $ProcessObj = Forks::Super::Job::get($pid);
while( $ProcessObj->is_active() ) {
if( $run_cmd_die == 1 ) {
$ProcessObj->kill(1);
}
}
This would never get out of the while loop.
My final solution has been to modify Proc::Background
to fill my needs. I added a new optional entry to the options hash and passed the options hash through to the _new
subroutines for Win32 and Unix. Then I can provide the directory to the Win32::Pro开发者_Go百科cess::Create call for Win32 and use a chdir for Unix.
A naive implementation would be to just chdir before you run the Proc::Background function to start the other process and then immediately chdir back.
Forks::Super
v0.51 supports all five of these features.
use Forks::Super;
# launch $command $arg1 $arg2 in background, starting from dir $directory
$pid = fork { dir => $directory, cmd => [ $command, $arg1, $arg2 ], ... };
# check if process is still running
if (! $pid->is_complete) { ... }
# kill a process. Straightforward on UNIX, DWIM on Win32
$num_signalled = kill 'TERM', $pid;
$pid->kill($signal); # alternate
精彩评论