Kill a system process within the script

M

Mav

Hi, all
I am trying to lanuch a command on my perl script using system call,
I wonder is that a way when someone hit Ctrl-Y, it will kill my
script,and also kill that system call process as well.

My script:

....
@args = ("doing something take a long time");

if (system(@args)==0) {
print "ok";
} else
print "something wrong";

Please help,
Thanks,
M
 
G

Gregory Toomey

Mav said:
Hi, all
I am trying to lanuch a command on my perl script using system call,
I wonder is that a way when someone hit Ctrl-Y, it will kill my
script,and also kill that system call process as well.

Huh? killing a process takes microseconds.
My script:

...
@args = ("doing something take a long time");

if (system(@args)==0) {
print "ok";
} else
print "something wrong";

Please help,
Thanks,
M

Assuming linux/unix:
Try system("kill -SIGTERM $process") ; then
Try system("kill -SIGKILL $process")

where you set $process top the pid you want tokill.


gtoomey
 
A

Anno Siegel

Mav said:
Hi, all
I am trying to lanuch a command on my perl script using system call,
I wonder is that a way when someone hit Ctrl-Y, it will kill my
script,and also kill that system call process as well.

My script:

...
@args = ("doing something take a long time");

if (system(@args)==0) {
print "ok";
} else
print "something wrong";

I don't know what Ctrl-Y does in your system. An INT signal (as
created by Ctrl-C per default on Unix) is already treated the way
you want.

Anno
 
L

lee

Mav said:
I am trying to lanuch a command on my perl script using system call,
I wonder is that a way when someone hit Ctrl-Y, it will kill my
script,and also kill that system call process as well.

Yes.

When you launch the process, note its process ID.

Then have your script "trap" the control character in question,
and when that character is received, call the system 'kill'
command
on the previously noted PID, as described by someone else on
this thread.

See, for example, perldoc -q signal:

Found in .... pod/perlfaq8 :
How do I trap control characters/signals?

$Interrupted = 0; # to ensure it has a value
$SIG{INT} = sub {
$Interrupted++;
syswrite(STDERR, "ouch\n", 5);
}


Lee Goddard
 
M

Mav

The script is running on the PC.
Once I launch the "system("Doing something take a long time");" from
my perl script(doit.pl), I hope when the user kill my doit.pl, it will
kill the
"system("Doing something take a long time");" process as well, I
really don't want leave the process behind.

Is that anywhere I can find code example doing such thing?

Thanks a lot,
Mav
 
M

Mothra

Mav said:
The script is running on the PC.
Once I launch the "system("Doing something take a long time");" from
my perl script(doit.pl), I hope when the user kill my doit.pl, it will
kill the
"system("Doing something take a long time");" process as well, I
really don't want leave the process behind.

Is that anywhere I can find code example doing such thing?
Try Chapter 16 of "Perl Cookbook" published by O'Reilly - there's loads
of examples in there.
 

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

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,821
Latest member
AleidaSchi

Latest Threads

Top