Perl script to kill the process ID of a command after a certain period


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Perl script to kill the process ID of a command after a certain period
# 1  
Old 11-27-2009
Perl script to kill the process ID of a command after a certain period

All,

I am trying to build a script in perl that will alllow me to pass the IP address to a ping command and redirect the output to a file and then kill that process after a certain period of time.

let's say, I call my script ping.pl, I would like to be able to run it like this for example : ./ping.pl 127.0.0.1 60 (where 127.0.0.1 will be the ip address I want to run the ping command on and 60 will be the period is seconds I would like the ping command to run and kill process ID for this ping command after this period).

I started out like this, but it looks like when I enter the command ./ping.pl 127.0.0.1 60. Nothing never happens. When I type in the IP, hit enter and type 60, hit enter. It starts working. It creates the file, but it never stops running. The process has never been killed. Any help will be appreciated.

I would like to be able to run the script on one line command with the arguments like ./ping.pl 127.0.0.1 60 ( it could be any IP and any time).

Thanks,

#!/usr/bin/perl
$i = <STDIN>;
$j = <STDIN>;
system("ping $i > ping.pl.txt");
system("sleep $j");
system("kill -9 `ps aux |grep ping|awk '{ print $2 }' ");
# 2  
Old 11-27-2009
i see you use system tools within your Perl script. If that's the case, ditch this ugly method, and do everything with a shell script. Use $1, $2 as your arguments.
# 3  
Old 11-28-2009
Quote:
Originally Posted by Pouchie1
...
#!/usr/bin/perl
$i = <STDIN>;
$j = <STDIN>;
system("ping $i > ping.pl.txt");
system("sleep $j");
system("kill -9 `ps aux |grep ping|awk '{ print $2 }' ");
Ugh... That looks as out of place as Pavarotti in a Bon Jovi concert. Please do yourself a favor and use a shell script.

tyler_durden
# 4  
Old 11-28-2009
I'm not if the perl system commands are the exact same as shell commands, butif they are, the ping command will just keep pinging forever, so the script is effectively stuck on the ping line and never gets to the sleep or kill lines.

Use "ping -c " to ping a certain number of times, e.g. "ping -c 2 google.com" will ping twice, or use "&" to put the ping line in the background.

Try this shell script:
Code:
#! /bin/sh
ping $1 >> ping.txt &
sleep $2
kill -9 `ps aux | grep ping | awk '{ print $2 }'`

# 5  
Old 11-28-2009
The proper way is to record the pid of the child processes that need to be monitored and use that in the kill operation.
Code:
#!/bin/sh
ping $1 > ping.sh.txt &
pid=$!
sleep $2
kill $pid

Also, liberal use of kill -9 is not good practice and this should be avoided. A simple killl should suffice, perhaps with an additional check that may lead to further action.

Perhaps we should call it: UUoK-9 Smilie

Also - in situations where there still is a need to get the pid off the process list - note that with
Code:
kill $(ps aux | grep ping | awk '{ print $2 }' )

the grep process gets selected and killed too..
You can avoid that by using
Code:
kill $(ps | grep ping | grep -v grep | awk '{print $1}' )

or more efficiently (and since this is a child process there is no need for the arguments to ps)
Code:
kill $(ps | grep pin[g] | awk '{print $1}')

or more efficiently
Code:
kill $(ps | awk '/ping/{print $1}')

or if you do not need portability and you system supports it:
Code:
kill $(pgrep ping)

Or if you are feeling lucky:
Code:
pkill ping

Of course with some pings there is no need for kill since you can just specify how long it should be active, so you could e.g. just use
Code:
#!/bin/sh
ping $1 -w $2 > ping.sh.txt &


Last edited by Scrutinizer; 11-28-2009 at 05:57 AM..
# 6  
Old 11-28-2009
Thanks all for the feedback.

Scrutinizer,

Your post really scrutinize the problems and give me all kind of possible solutions. It's really appreciated. I cannot ask for more.

The only thing is that it looks like there is no simple way we can have perl do the same thing. is that a fair statement?
# 7  
Old 11-28-2009
Thanks Pouchie1. No I think it can be done using Perl. You could probably use Net::Ping (I haven't used it myself). What is being said is that if you are going to use system tools to accomplish your goal then effectively you are using almost only external commands and you might as well use a shell script instead.

Last edited by Scrutinizer; 11-30-2009 at 02:01 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Command to know all the Current running process and how to kill

All, 1.What is the unix comand used for all current running process (Including All current running processes Parent ->child->subchild process) 2.If child and subchild processes are running then what is the unix command to kill parent and its all child subchild processes in UNIX. Kindly... (7 Replies)
Discussion started by: skp
7 Replies

2. Shell Programming and Scripting

perl check and kill process

Hi All How can i in perl check for a running process and if it is running kill it the issue might be that there will be more than one of the same process and i want to kill all of them the process is below root 1944 1 0 16:28 ? 00:00:01 x11vnc -display :0... (3 Replies)
Discussion started by: ab52
3 Replies

3. UNIX for Dummies Questions & Answers

Script to start background process and then kill process

What I need to learn is how to use a script that launches background processes, and then kills those processes as needed. The script successfully launches the script. But how do I check to see if the job exists before I kill it? I know my problem is mostly failure to understand parameter... (4 Replies)
Discussion started by: holocene
4 Replies

4. Shell Programming and Scripting

Script to Kill a Process by Name...

Hello all... new to these forums and a bit of a newbie with linux aswell. I need to figure out how to write a shell script to kill a process by name as given to the script as an argument. I've got that part working OK, but i need to make sure that the script does not allow processes that are... (6 Replies)
Discussion started by: cannon1707
6 Replies

5. Shell Programming and Scripting

Shell Script to Kill Process(number of process) Unix/Solaris

Hi Experts, we do have a shell script for Unix Solaris, which will kill all the process manullay, it used to work in my previous env, but now it is throwing this error.. could some one please help me to resolve it This is how we execute the script (and this is the requirement) ... (2 Replies)
Discussion started by: jonnyvic
2 Replies

6. Shell Programming and Scripting

Script to kill process...

hello Bros, I need to write some script that i can put it on crontab which checks for a process X if running. If the process X is ruuning then take the PID and kill it or display message that says process X is not running. I am using AIX 5.3 Thanks guys.:b: (2 Replies)
Discussion started by: malcomex999
2 Replies

7. Linux

Kill a process without using kill command

I want to Kill a process without using kill command as i don't have privileges to kill the process. I know the pid and i am using Linux 2.6.9 OS. (6 Replies)
Discussion started by: sudhamacs
6 Replies

8. Shell Programming and Scripting

Kill a process without using kill command

Sorry, posted the question in other forum. (0 Replies)
Discussion started by: sudhamacs
0 Replies

9. Shell Programming and Scripting

Perl : Kill process within 5 min

From a perl script , How can I monitor a PS which I activated and kill it within 5 minutes in case it didn't complete its tasks.:confused: (2 Replies)
Discussion started by: Alalush
2 Replies

10. Shell Programming and Scripting

to kill a process in perl

Hi friends, I have a perl script which needs to kill all java processes running on both windows and unix. currently I'm getting the OS and killing the process by using system. my code is: if ($os eq MSWin32) system("taskkill java"); else system("kill -9 java") Is there any way... (2 Replies)
Discussion started by: gurukottur
2 Replies
Login or Register to Ask a Question