process ids

R

Robert Mohr

Is there a way for a program outside of Perl to be run in the background
and have Perl store the process id. That is, if I want to run the
program 'program', I could run something similar to `program &` and get
something like '[1] 12834' from it (what I'd get from running 'program
&' from the command line).
 
B

Ben Morrow

Robert Mohr said:
Is there a way for a program outside of Perl to be run in the background
and have Perl store the process id. That is, if I want to run the
program 'program', I could run something similar to `program &` and get
something like '[1] 12834' from it (what I'd get from running 'program
&' from the command line).

(untested)

use POSIX qw/:sys_wait_h/;

$SIG{CHLD} = sub { 1 while 0 < waitpid -1, WNOHANG };

my $pid = fork;
defined $pid or die "can't fork: $!";
$pid or exec "program";

Ben
 
R

Robert Mohr

Ben said:
Robert Mohr said:
Is there a way for a program outside of Perl to be run in the background
and have Perl store the process id. That is, if I want to run the
program 'program', I could run something similar to `program &` and get
something like '[1] 12834' from it (what I'd get from running 'program
&' from the command line).

(untested)

use POSIX qw/:sys_wait_h/;

$SIG{CHLD} = sub { 1 while 0 < waitpid -1, WNOHANG };

my $pid = fork;
defined $pid or die "can't fork: $!";
$pid or exec "program";

Thank you. One more question though: how do you kill the process from
the id? Can it be done through Perl (I assume it can, but can't find
out how from perldoc POSIX, which is rather confusing) or must I do it
through sh?
 
P

Paul Lalli

Thank you. One more question though: how do you kill the process from
the id? Can it be done through Perl (I assume it can, but can't find
out how from perldoc POSIX, which is rather confusing) or must I do it
through sh?

perldoc -f kill


Paul Lalli
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,122
Latest member
VinayKumarNevatia_
Top