assign background process to variable

L

lancerset

Hello everyone,

I know in shell scripting you can put a job in the background and get
that process id and assign it to a variable, have it capture some data
and
theN kill it. I cant seem to get it going in perl. What is the perl
equivilent.

#!/bin/sh

/usr/sbin scriptgoestobg &
PID=$!
sleep 5
kill -9 $PID


Thanks again...
 
P

Paul Lalli

Hello everyone,

I know in shell scripting you can put a job in the background and get
that process id and assign it to a variable, have it capture some data
and
theN kill it. I cant seem to get it going in perl. What is the perl
equivilent.

#!/bin/sh

/usr/sbin scriptgoestobg &
PID=$!
sleep 5
kill -9 $PID

What have you tried so far? How did it not work?

I would suggest forking a new process, exec'ing the external job in the
child process, and killing the process_id in the parent whenever you've
decided the external job should die.

You may also wish to read `perldoc -f system` to see what that command
*actually* does.

Paul Lalli
 
R

robic0

Hello everyone,

I know in shell scripting you can put a job in the background and get
that process id and assign it to a variable, have it capture some data
and
theN kill it. I cant seem to get it going in perl. What is the perl
equivilent.

#!/bin/sh

/usr/sbin scriptgoestobg &
PID=$!
sleep 5
kill -9 $PID


Thanks again...

Win32::process::Create();
$asdfasdf = GetProcessID();
Win32::process::KillProcess($asdfasdf);
 
J

John W. Krahn

I know in shell scripting you can put a job in the background and get
that process id and assign it to a variable, have it capture some data
and
theN kill it. I cant seem to get it going in perl. What is the perl
equivilent.

#!/bin/sh

/usr/sbin scriptgoestobg &
PID=$!
sleep 5
kill -9 $PID

#!/usr/bin/perl
use warnings;
use strict;

my $program = 'scriptgoestobg';

defined( my $pid = fork ) or die "Cannot fork: $!";

exec $program or die "Cannot exec $program: $!" unless $pid;

sleep 5;

kill 9, $pid;

__END__



John
 
J

Josef Möllers

robic0 said:
Win32::process::Create();
$asdfasdf = GetProcessID();
Win32::process::KillProcess($asdfasdf);

Very nice, indeed, but the fact that the OP posted a shell script should
have indicated, that a Windows answer wasn't what he was looking for ;-)
 

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,780
Messages
2,569,608
Members
45,241
Latest member
Lisa1997

Latest Threads

Top