Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

kill process when file count reached,,.

1 view
Skip to first unread message

onlineviewer

unread,
Jul 1, 2008, 7:25:44 PM7/1/08
to
Hello All,

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

#!/usr/bin/perl

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

Keith

unread,
Jul 2, 2008, 8:58:10 AM7/2/08
to
> }- Hide quoted text -
>
> - Show quoted text -

And the problem is....????

Leon Timmermans

unread,
Jul 2, 2008, 9:10:43 AM7/2/08
to
On Tue, 01 Jul 2008 16:25:44 -0700, onlineviewer wrote:

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

Hi,

I think this should have been posted at comp.lang.perl.misc instead of
here. Please think of that next time.

> #!/usr/bin/perl
>

Please use strict and warnings, specially when asking for help. It
prevents a lot of bugs.

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

From the documentation of system():
"Does exactly the same thing as "exec LIST", except that a fork is done
first, and the *parent process waits for the child process to complete*."
That's not going to work.

> while(true){

true? This is perl, not a shell script. Also, you might want to move that
sleep into the loop, or else it will burn CPU time. Also, you're having
an infinite loop there, which you probably don't want.

> @array1 = `ls -l /tmp | grep files`;

IMO you could just use readdir and grep in perl. That should be strongly
preferred over using backticks (alternatively you could use File::Find,
but I'd say that's an overkill in this case). Further the names don't
match ('file.out' vs 'files'), which is definitely a bug. Also please
don't call your array @array1, it's probably the worst possible name you
could give it.

> $result=@array1+1;

What does the +1 serve for??

> if ($result > 3){

In your text you talk about 10 files, but this only checks for 2? Why?

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

You're using awk in backticks in a perl script? Make a choice: shell
scripting or perl scripting. Also, there is a pidof program you could
have used, but a better approach would have been to use fork & exec, in
that case you wouldn't have had this problem in the first place.

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

This looks a bit double and broken...

> system "kill -9 $c";

Perl has a kill builtin, why the hell not use that. Also, is a -9 really
necessary?

> print "killing tcpdump...";
> }else{
> print "!!!\n";
> exit;

If there are fewer than 3 files, it exits, if there are more, it will go
into infinite loop. That's got to be an error.


How about this program?

#!/usr/bin/perl

use strict;
use warnings;

defined(my $pid = fork) or die "Couldn't fork: $!\n";
if ($pid) {
while (1) {
sleep 2;
opendir my $dir, "/tmp" or die "Couldn't open /tmp: $!\n";
my @files = grep { /file.out/ } readdir $dir;
closedir $dir;
if (@files >= 10) {
print "killing tcpdump...\n";
kill 'TERM', $pid;
last;
}
else {
print "!!!\n";
}
}
}
else {
else exec 'tcpdump -i bge1 -s0 -w /tmp/file.out -C 1';
}


Leon

John W. Krahn

unread,
Jul 2, 2008, 12:56:05 PM7/2/08
to
Leon Timmermans wrote:
>
> How about this program?
>
> #!/usr/bin/perl
>
> use strict;
> use warnings;
>
> defined(my $pid = fork) or die "Couldn't fork: $!\n";
> if ($pid) {
> while (1) {
> sleep 2;
> opendir my $dir, "/tmp" or die "Couldn't open /tmp: $!\n";
> my @files = grep { /file.out/ } readdir $dir;

The . meta-character is special in a regular expression but you probably
want to match a literal period instead. Also you only need the count of
files and not a list of the actual file names so just use a scalar instead.

my $count = grep /^file\.out/, readdir $dir;


> closedir $dir;
> if (@files >= 10) {
> print "killing tcpdump...\n";
> kill 'TERM', $pid;
> last;

You could probably just exit() (or maybe POSIX::_exit()) here instead?


> }
> else {
> print "!!!\n";
> }
> }
> }
> else {
> else exec 'tcpdump -i bge1 -s0 -w /tmp/file.out -C 1';

Probably better to use a list instead of a string so you don't invoke a
shell to run tcpdump:

else exec 'tcpdump', '-i', 'bge1', '-s0', '-w', '/tmp/file.out',
'-C', '1';


> }


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

Leon Timmermans

unread,
Jul 2, 2008, 6:43:54 PM7/2/08
to
On Wed, 02 Jul 2008 16:56:05 +0000, John W. Krahn wrote:

>
> The . meta-character is special in a regular expression but you probably
> want to match a literal period instead.

Oops...

>
> You could probably just exit() (or maybe POSIX::_exit()) here instead?
>

Matter of style. I prefer using last in such situations because it
generally makes it easier to make a subroutine out of it later.

>
> Probably better to use a list instead of a string so you don't invoke a
> shell to run tcpdump:
>
> else exec 'tcpdump', '-i', 'bge1', '-s0', '-w', '/tmp/file.out',
> '-C', '1';

Actually that doesn't matter here. If the command doesn't contain shell
metacharacters it is not passed to the shell (see perlfunc). That makes
it a matter of aesthetics.

Leon

comp.llang.perl.moderated

unread,
Jul 3, 2008, 8:15:05 PM7/3/08
to

In case someone might forget, the parent'll need
a 'wait' or 'waitpid' too.

A quick 'n dirty alternative although I'm still
not sure how the multiple output files are
named:

system("tcpdump... &");
sleep 2 while (()=glob("/tmp/...")) < 10;
kill 'TERM', -$$;

--
Charles DeRykus

comp.llang.perl.moderated

unread,
Jul 3, 2008, 8:32:45 PM7/3/08
to
On Jul 3, 5:15 pm, "comp.llang.perl.moderated" <c...@blv-


I forgot to mention that this kills both
the backgrounded tcpdump and the perl
script itself. That seemed like the sole
goal though.

--
Charles DeRykus
> Charles DeRykus

0 new messages