Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling

A wrapper script for tmpwatch in Linux written in Perl

by wishartz (Beadle)
on Jul 03, 2007 at 09:11 UTC ( #624624=perlquestion: print w/replies, xml ) Need Help??
wishartz has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monk's, could somebody please help me. I am trying to make a wrapper script for the linux tmpwatch command, which removes files which havent been accessed for 'x' number of days from certain file systems. The problem is I need to ssh on to certain hosts to run the tmpwatch command, or it will take forever to remove the files. It's proving hard to get the host name from the data structure I have made. The ssh part does not work because when it tries to retrieve the host name, it doesn't dereference it properly. This is what I have so far.
use warnings; use vars qw( $choicesystem $choicehours $option $manager $host %hours $hourkey $row ); $host = `hostname`; our %filesystem = ( 1 => { name => "/filesystema", host => { namehost1 => 1 }, }, 2 => { name => "/filesystemb", host => { namehost2=> 2 }, }, 3 => { name => "/filesystemc", host => { namehost3 => 3 }, }, ); %hours=(1=>"48",2=>"96"); #print Dumper(%filesystem); debug chomp($host); print "$host\n"; print "----------------Menu-----------------------------------------\n +"; print "Please choose from the list of file systems\n"; print "-------------------------------------------------------------\n +"; for $row (sort keys %filesystem){ my $system=$filesystem{$row}; print "$row\t $system->{ name }\n"; } print "Please enter number>"; chomp($choicesystem = <STDIN>); my $system = $filesystem{ $choicesystem }; print "----------------Menu-----------------------------------------\n +"; print "Please choose the number of hours\n"; print "-------------------------------------------------------------\n +"; foreach $hourkey (keys %hours){ print "$hourkey\t $hours{$hourkey} \n" } print "Please enter number>"; chomp($choicehours = <STDIN>); my $choicehours = $hours{$choicehours}; if ( not $system->{host}{$host}){ system "/usr/bin/ssh $system->{host}#this part doesn't work, o +nly brings back reference"; print "/usr/bin/sudo /usr/sbin/tmpwatch - +f $choicehours $system->{name}\n"; } else { print "/usr/bin/sudo /usr/sbin/tmpwatch -f $choicehours $system +->{name}\n";}
I think the data structure that I've used needs to be changed, because the host name is a key rather than a value. I just don't know how to do it. Any help is appreciated. Thanks.

Replies are listed 'Best First'.
Re: A wrapper script for tmpwatch in Linux written in Perl
by bloonix (Monk) on Jul 03, 2007 at 11:18 UTC
    system "/usr/bin/ssh $system->{host}#this part doesn't work, o+nly brings back reference";
    of course! $system->{host} is a hash reference! you need
    to access the hostname.
      I tried that and it doesn't work because it only enters that part of the if condition, if $host (the machine you are logged into at that time, which I have set with $host =`hostname`) doesn't equal $system->{host}
      if ( not $system->{host}{$host})
      I need to be able to dreference $system->{host} so it will come up with the name of the hostname, but I don't think that will work because the hostname is the key rather than the value. I think I might have to change my hash. Thanks for your help anyway.
Re: A wrapper script for tmpwatch in Linux written in Perl
by john_oshea (Priest) on Jul 03, 2007 at 12:22 UTC

    Somewhat off-topic, but would it not be simpler to run tmpwatch out of a cron job on each machine? That way you could pretty much forget about it...

    This, of course, assumes that you can determine sane values for it on each machine that cater for those oh-so-pesky users... ;-)

      We already run a cronjob on each machine which runs tmpwatch, but sometimes with the environment I work in, some of the jobs use so much space that it needs to be run manually sometimes and this could be early in the morning. This script is for the operators work in shifts 24/7 and may need to run a tmpwatch to get rid of some of the files. My manager wants things simplified for them so they just run a script and choose from the menu options, to clean up some of the space.
        Can anybody suggest a better hash structure to use for this program? I've really hit a deadend with this. Thanks

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://624624]
Approved by planetscape
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (5)
As of 2017-06-24 08:59 GMT
Find Nodes?
    Voting Booth?
    How many monitors do you use while coding?

    Results (557 votes). Check out past polls.