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