views:

250

answers:

3

I want to write a Perl code that ends the process of another program of mine, running on Linux.

For example, I execute xxy.py and a process is opened for it. I would like to use Perl to close the process of xxy.py. What do I need to do?

A: 

See kill(1) and pkill(1) manual pages.

Nikolai N Fetissov
A: 

use killall to kill processes by process names. if you have to use perl. do a system call from within there.

neal aise
could anyone explain the negatives here?
neal aise
Not a downvoter, but perhaps it's because only the FreeBSD-style `killall` cares about the name given. SysV-style `killall` is much more literal and will attempt to kill **all** processes. (Or, given that this is SO, they could just be random drive-by downvotes...)
Dave Sherohman
thanks Dave. you figure right. i come from a freebsd background. din't know killall doesnt work the same. would definitely try killall if i get to a linux system. sounds a crazy function.the good news is that someone had sympathy to my downvotes :)
neal aise
+3  A: 

Perl extensions are typically .pl or .pm right? .py is for python I think.

Anyway, you can kill a specified program in a Unix environment with something like:

system 'killall', 'some_program_name';

or

system 'kill', '-15', $pid;

if the variable $pid holds the pid of your program.

KLee1
A better way to write that is `system 'kill', '-9', $pid;`. That will avoid going through a shell
friedo
Additionally, you should probably avoid using `-9` (`SIGKILL`) as it doesn't give the process you're killing any chance to clean itself up. `SIGTERM` (usually `-15`) is better.
friedo
You're right. I will edit my post to reflect this. I was just used to having to kill unresponsive programs with 9.
KLee1
perl has a kill builtin; use it instead of an external program...
ysth