Click here to get back home

kill process when file count reached,,.

 HomeNewsGroups | Search | About
 comp.lang.perl.modules    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 count reached,,. onlineviewer 07-01-2008
Posted by onlineviewer on July 1, 2008, 7:25 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 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;
}
}

Posted by Keith on July 2, 2008, 8:58 am
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 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){
> =A0 =A0 =A0 =A0 @array1 =3D `ls -l /tmp | grep files`;
> =A0 =A0 =A0 =A0 $result=3D@array1+1;
>
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 if ($result > 3){
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 $x=3D`ps -ef | awk '/tcpdu=
mp/ && !/awk/ {print
> $2}'`;
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 @y=3Dsplit(' ', $x);
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 $c=3D$y[1];
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 system "kill -9 $c";
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 print "killing tcpdump..."=
;
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 }else{
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 print "!!!\n";
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 exit;
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 }
>
>
>
> }- Hide quoted text -
>
> - Show quoted text -

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


Posted by Leon Timmermans on July 2, 2008, 9:10 am
Please log in for more thread options
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

Posted by John W. Krahn on July 2, 2008, 12:56 pm
Please log in for more thread options
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

Posted by Leon Timmermans on July 2, 2008, 6:43 pm
Please log in for more thread options
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

Similar ThreadsPosted
Win32::Process Kill Process in Windows ME April 13, 2005, 1:49 am
Win32::Process, SetProcessAffinityMask for an existing process = perl crash May 7, 2006, 6:22 pm
How to retreive a command's output, monitor execution time and kill it July 14, 2004, 12:16 pm
Namespace is changed before "package" line is reached November 30, 2007, 1:49 pm
What to do when CPAN module authors can't be reached? (Bug + fix for Frontier::RPC2) May 6, 2006, 2:50 pm
Getting the name of the process via the process ID December 14, 2004, 7:14 am
Process syncronization in perl? November 6, 2004, 5:10 pm
fork process help needed March 15, 2007, 8:56 pm
starting a background process via Net::SSH::W32Perl September 7, 2004, 10:41 am
CPAN module validation process July 19, 2007, 3:58 pm

Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap