Click here to get back home

kill process when file number reached...

 HomeNewsGroups | Search | About
 comp.lang.perl.misc    Post an article   get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content
Subject Author Date
kill process when file number reached... onlineviewer 07-02-2008
Get Chitika Premium
Posted by onlineviewer on July 2, 2008, 2:45 pm
Please log in for more thread options
Hello All,

I am trying to run a tcpdump and have perl kill the tcpdump once 10
files have been created by the tcpdump. Here is my code, not sure...if
my logic is screwy
Thanks,

system "tcpdump -i bge1 -s0 -w /tmp/file.out -C 1";
sleep 2;

while(true){
@array1 = `ls -l /tmp | grep files`;
$result=@array1+1;

if ($result > 3){
$x=`ps -ef | awk '/tcpdump/ && !/awk/ {print
$2}'`;
@y=split(' ', $x);
$c=$y[1];
system "kill -9 $c";
print "killing tcpdump...";
}else{
print "!!!\n";
exit;
}
exit;

Posted by smallpond on July 2, 2008, 3:04 pm
Please log in for more thread options
onlineviewer wrote:
> Hello All,
>
> I am trying to run a tcpdump and have perl kill the tcpdump once 10
> files have been created by the tcpdump. Here is my code, not sure...if
> my logic is screwy
> Thanks,
>
> system "tcpdump -i bge1 -s0 -w /tmp/file.out -C 1";
> sleep 2;
>
> while(true){
> @array1 = `ls -l /tmp | grep files`;
> $result=@array1+1;
>
> if ($result > 3){
> $x=`ps -ef | awk '/tcpdump/ && !/awk/ {print
> $2}'`;
> @y=split(' ', $x);
> $c=$y[1];
> system "kill -9 $c";
> print "killing tcpdump...";
> }else{
> print "!!!\n";
> exit;
> }
> exit;



Why bother with perl? With the tcpdump command that you have, you
get about 100 bytes/packet. So if you want 10 x 1 MB, then you want
100,000 packets. -c 100000
** Posted from http://www.teranews.com **

Posted by Eric Pozharski on July 3, 2008, 3:34 pm
Please log in for more thread options
> Hello All,

> I am trying to run a tcpdump and have perl kill the tcpdump once 10
> files have been created by the tcpdump. Here is my code, not sure...if
> my logic is screwy

Your logic isn't screwy, it's misunderstanding.

> system "tcpdump -i bge1 -s0 -w /tmp/file.out -C 1";

I'm not the B<tcpdump> expert, but B<if> my understanding of tcpdump(8)
is right, then you'll never get out of B<system>. If you use I<-c> (as
smallpond suggested) then B<tcpdump> B<will> exit (apparently you don't
need B<kill> in that case).

OK, if you really want to write shell scripts in Perl do it in Perl.

> sleep 2;

I believe, you missed B<sleep> inside loop.

> while(true){

Show your B<real> code! What the fsck is that "true"?

> @array1 = `ls -l /tmp | grep files`;
> $result=@array1+1;

$result = (() = </tmp/file.out.*>) + 1;

(He-he, I was beaten hardly a week before for missing that.)

> if ($result > 3){

if((() = </tmp/file.out.*>) > 2)

and you don't need to increment.

> $x=`ps -ef | awk '/tcpdump/ && !/awk/ {print
> $2}'`;

$x = (map { m; }
map { readlink; }
</proc/[0-9]*/exe>)[0];

Since you seem to be root, you'll have permissions to read those
symlinks.

> @y=split(' ', $x);
> $c=$y[1];

$c = (split m, $x)[1];

> system "kill -9 $c";

kill 9, $c;
waitpid $c, 0;

> print "killing tcpdump...";

Use B<Proc::Background> and you'll automagically would know the PID of
B<tcpdump>. If you don't want to bother with B<Proc::Background>, then
C<perldoc perlfork> is good reading.

> }else{
> print "!!!\n";
> exit;

Either that must be B<last> or you don't need the second B<exit>.
Anyway you don't need the second B<exit> since you've better just fall
out of script.

> }
> exit;

And as ever: C<use strict> and C<use warnings> are your best friends.
Lexical filehandles and 3-arg B<open> are your good friends.

--
Torvalds' goal for Linux is very simple: World Domination

Posted by John W. Krahn on July 4, 2008, 12:53 am
Please log in for more thread options
Eric Pozharski wrote:
>>
>> @y=split(' ', $x);
>> $c=$y[1];
>
> $c = (split m, $x)[1];

split ' ' and split m do different things so the list element ()[1]
may not return the expected result depending on whether there is leading
whitespace in $x.


John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order. -- Larry Wall

Posted by Eric Pozharski on July 4, 2008, 2:15 pm
Please log in for more thread options
> Eric Pozharski wrote:
*SKIP*
>> $c = (split m, $x)[1];

> split ' ' and split m do different things so the list element ()[1]
> may not return the expected result depending on whether there is leading
> whitespace in $x.

I agree, but leading space in B<ps> output would be a big surprise.

--
Torvalds' goal for Linux is very simple: World Domination

Similar ThreadsPosted
kill the process July 7, 2006, 3:51 pm
how to kill a process initiated by system() October 27, 2004, 10:29 am
how to kill a spawned process when it hangs May 4, 2005, 4:10 am
How to kill a forked child process... June 17, 2005, 7:26 pm
sending a kill to child process August 6, 2005, 11:26 am
kill the ssh process called by the system command January 22, 2006, 7:29 pm
perl one-liners to search and kill process remotely May 2, 2006, 3:06 am
Lunge crowd, kill file November 19, 2005, 1:25 pm
Check if file is being modified by another process July 4, 2007, 11:21 am
grep in file and date process March 3, 2008, 7:42 am

Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap