Killing a process from perl script.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Killing a process from perl script.
# 1  
Old 04-01-2004
Power 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
# 2  
Old 04-01-2004
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.
# 3  
Old 04-01-2004
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
# 4  
Old 04-01-2004
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

Code:
[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.

Code:
...
...
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";
}
...
...

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Killing the process if running for long time in script

I am running a script which will read the data from fail line by line and call the Java program by providing the arguments from the each line. The Java code is working fast for few records and for some records its getting hanged not providing response for morethan one hour. Currently am... (4 Replies)
Discussion started by: dineshaila
4 Replies

2. Shell Programming and Scripting

Automated Script for Process killing

Hello all, I'm in need of a Script which needs to wait for all the child process to end and then kill the main process. I have a process called mainpp which runs for different instances like evpn, nge, gmn etc so when i query for mainpp process it looks like below. bash-3.2$ ps -eaf |... (6 Replies)
Discussion started by: Mahesh_RPM
6 Replies

3. Shell Programming and Scripting

Killing a bash process and running the second part of script

I want to run a script that calls remote ssh and if it gets hung, I want to be able to kill that part of the script and run another command in the script for example I have a statement like this: if ]; then export tapes=$(for tape in $(su - nacct -c 'ssh remote1 "cat... (1 Reply)
Discussion started by: newbie2010
1 Replies

4. Homework & Coursework Questions

Unix Script -- To Skip killing a specific process

Hi, By using ps -aux | awk '/mine/{split($15,a,"/");print $1,$2,a}' i get the below listed PID's with there corresponding processes. adm 1522 ABC_Process.tra adm 1939 GENE_Process.tra adm 2729 GENE_Archive.tra adm 3259 xyz_Process.tra I use ps -aux | awk... (5 Replies)
Discussion started by: murali1687
5 Replies

5. Shell Programming and Scripting

**need help for killing a process through script**

Hello All, i hope you are fine. I need a little help from you people-- inside a script i want to kill a parent process by checking it with the child process.. p_pid=`ps -e | awk '/ra_cmd_d/ {print$1}'` here i am selecting the child process id in p_pid. next-- sleep_pid=`ps -af |... (3 Replies)
Discussion started by: onlyniladri
3 Replies

6. Shell Programming and Scripting

Killing a process within 5 min it starts in Unix using perl

Hi All, I have to kill a program whose pid, i will be getting. Multiple processes will be getting started by my script of same kind in a series. So for after each call to a process i need to write a command or script which can kill the process if it takes more than 5min. In this i will... (3 Replies)
Discussion started by: nishank.jain
3 Replies

7. Shell Programming and Scripting

script not killing process!

i am using script to connect remotly to server and run some commands , one of these commands is to kill some process but tried different ways with no hope sshpass -p 'pass' ssh -o StrictHostKeyChecking=no server kill -9 `pgrep procs` getting error message "kill: bad argument count" ... (2 Replies)
Discussion started by: mogabr
2 Replies

8. Shell Programming and Scripting

While killing the process, Script gets hanged. Please help me on this.

Hi Everyone, I have written a script to automate the docbase cleanup process on every weekly basis. That is in this script i have to shutdown the docbase and then kill all the process that are hanged except one process(epic process) and need to delete some log files and then i have to start the... (8 Replies)
Discussion started by: Sheethal
8 Replies

9. AIX

killing a process from a script

Hey all. I'm brand new to this forum and am looking for some help. I have a script that verifies that the backup tapes are working correctly. Basically is uses 1 command: restore -xpqvf > rootvglog I use this for each volume group that we have. We run this everyday but the problem is, we... (4 Replies)
Discussion started by: jalge2
4 Replies

10. Shell Programming and Scripting

killing process using a script

can I do ps -ef | grep <process_name> and kill the process is it exists? and send a mail to me that the process was found and killed Thanks much... KS (4 Replies)
Discussion started by: skotapal
4 Replies
Login or Register to Ask a Question