Killing a process from perl script.

How do I kill a process, say by name "drec" from a perl script.

I tried with :
ps -eaf | grep drec | awk '{print $2}' | xargs kill -9.

The output I got is :
ps -eaf | grep drec | awk '{print }' | xargs kill -9
/usr/bin/kill[8]: ipgen: Arguments must be %job or process ids

{But, $2 is not getting seen.}

Same thing worked from a shell script.

Thanks,
Sharath

I don't think you have provided enough info for anyone to help you. The command you listed looks more like something you would run from command line or in a ksh, bash, sh, or csh shell script. It definitely does not look like perl to me. Perl has it's own process handling functions which you should check into.

Sorry for incomplete question, Tony.

I was using perl's expect module to login to a machine and grep for the process and kill it.

That is when I faced this problem.

Now I am using cut command to kill the process and it is working fine.

But I am not satisfied with the cut command's way of handling my problem as I am using as below :

$cmd = "ps -eaf | grep drec | grep -v grep | cut -c10-15 | xargs kill -9";
print $log " $cmd\n";

This is solving my problem temporarily.
But I want a permanent solution.

Sharath

try to avoid all the exec calls to the shell. they will take a toll on your perl program.

This is a snipit from Learning Perl
section 14.7. Sending and Receiving Signals

[328]Sending a signal will also fail if you're not the superuser and it's someone else's process. It would be rude to send SIGINT to someone else's programs, anyway.

unless (kill 0, $pid) {
  warn "$pid has gone away!";
}

altho i dont kill a remote process i do kill my local process like so.
i know there are other modules out there to handle what i am trying to do but this seems to work for now.

...
...
if (-f "/tmp/ftp_out.pid") {
open (PIDFILE, "/tmp/ftp_out.pid");
chomp(my $PID=<PIDFILE>);
if (grep /$PID/, `ps -p $PID`) { close (PIDFILE); die "$0 is already running.\n"
; };
unlink "/tmp/ftp_out.pid";
}
...
...