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
Get Chitika Premium
Posted by comp.llang.perl.moderated on July 3, 2008, 8:15 pm
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';
>
> }

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


Posted by comp.llang.perl.moderated on July 3, 2008, 8:32 pm
Please log in for more thread options
On Jul 3, 5:15 pm, "comp.llang.perl.moderated" <c...@blv-
sam-01.ca.boeing.com> wrote:
>
>
>
> > 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';
>
> > }
>
> 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', -$$;


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


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
how to know that the process is idle ? November 18, 2008, 6:38 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

Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap