kill process when file number reached...

O

onlineviewer

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;
 
E

Eric Pozharski

onlineviewer said:
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.

I believe said:
while(true){

Show your B said:
@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{(\d+)}; }
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{\s+}, $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
then said:
}else{
print "!!!\n";
exit;

Either that must be B<last> or you don't need the second B<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.
 
J

John W. Krahn

Eric said:
onlineviewer said:
@y=split(' ', $x);
$c=$y[1];

$c = (split m{\s+}, $x)[1];

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


John
 
E

Eric Pozharski

John W. Krahn said:
Eric Pozharski wrote: *SKIP*
$c = (split m{\s+}, $x)[1];
split ' ' and split m{\s+} 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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,021
Latest member
AkilahJaim

Latest Threads

Top