in reply to Get the process id, and output of a process, in a threaded environment
but now I would like to be able to kill the process started up from this call programmatically. For this, I need the pid, which the open call so kindly returns. Unfortunately setting the environment variables before hand, causes the open to return the process ID of the shell, and not the actual process.
This problem really has nothing to do with threads. The same problem will manifest itself in a single threaded process. Ie. If you use the shell to invoke the executable, you'll get the pid of the shell not the executable.
The solution is actually quite simple. Don't use the shell.
You are using the shell in order to set up the environment that the executable will inherit. But, if you set the environment of your process to that you want the executable to inherit and then spawn the executable directly -- bypassing the shell -- you achieve your goal.
Ie:
{ local %ENV = %ENV; ## make your additions (deletions, changes) here; $ENV{FOO} = 'BAR'; my $pid = open my $pipe, '-|', '/path/to/the/executable', @args or + die ...; sleep $someTime; kill KILL, $pid; } ## the changes to %ENV are undone once you reach here
That's untested pseudocode.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Get the process id, and output of a process, in a threaded environment
by choroba (Cardinal) on Sep 05, 2013 at 21:14 UTC | |
|
Re^2: Get the process id, and output of a process, in a threaded environment
by rmahin (Scribe) on Sep 05, 2013 at 21:35 UTC | |
by BrowserUk (Patriarch) on Sep 05, 2013 at 21:47 UTC | |
by rmahin (Scribe) on Sep 05, 2013 at 22:08 UTC | |
by BrowserUk (Patriarch) on Sep 06, 2013 at 11:28 UTC | |
by rmahin (Scribe) on Sep 07, 2013 at 03:50 UTC |