开发者

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

  1. Run the process asynchronously.
  2. Be able to tell if the process is still running.
  3. Be able to kill the process.
  4. Have support for both Unix/Linux and Win32.
  5. 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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜