![]() |
Perl "re-execute" sub process
I wrote a perl script to remove the oldest files from a directory, until the partition reaches a desired size. The only problem I'm getting now, is getting the sub process to run again, as to find the next oldest file.
How do I run this, so that, if the partition is not yet under 83%, that it finds the next oldest file? Thanks == BEGIN SCRIPT == #!/usr/bin/perl # # New script to remove the oldest file in order to # get the partition size under 83% # use strict; use warnings; use File::Find; # my $userName = $ENV{'USER'}; if ($userName ne "root") { exit 1; } # my $DiskPart = 0; my $maxSize = 83; my $DiskDir = "/var/log/check"; # $DiskPart = "/var/log/check"; # # Determine current usage on log partition # my $partSize1 = `( /bin/df '$DiskPart' | /bin/egrep -v -i filesystem )`; my @partSizeArray = split(' ', $partSize1); my $partSize = `( /bin/echo '$partSizeArray[4]' | /usr/bin/tr -d % )`; undef @partSizeArray; # @ARGV=qw(".") unless @ARGV; my $oldestName = ""; my $oldestDate = 0; # # sub process that finds the oldest file # sub findOldFile { return unless -f $_&& -M $_ > $oldestDate; $oldestDate = -M $_; $oldestName = $File::Find::name; } # # Keep deleting files until partition is less than, or equal to $maxSize # until ($partSize <= $maxSize) { find (\&fildOldFile, "$DiskDir"); unlink($oldestName); $partSize1 = `( /bin/df '$DiskPart' | /bin/egrep -v -i filesystem )`; @partSizeArray = split(' ', $partSize1); $partSize = `( /bin/echo '$partSizeArray[4]' | /usr/bin/tr -d % )`; } == END SCRIPT == |
Are you sure that the loop is running even once? A few things jump out at me. One is that you're overquoting - putting variables in double quotes when you don't need quotes, using double quotes where you only need single quotes (no interpolation) and using single quotes where I'm not sure that you want any quotes (inside the shell command). Second, I think that you can simplify how you get the partition size. Third, is /var/log/check mounted separately? Otherwise, I don't think that you can run df on part of a filesystem, can you? (I may be completely wrong about that part, but it seems true on my system. If I run df /var/log, I get the same df output I would get for all of /dev/sda2 where /var/log lives.)
Here's a simplified version of the start of the script that you can use for debugging - that is, just to make sure that you are getting sane values stored in the initial part of the script. (In the future, please put your code in code blocks. It makes it much easier to read.) Code:
#!/usr/bin/env perlCode:
#!/usr/bin/env perl |
It might be easier to use a Perl module such as FileSys:: Df. you might want to try using an infinity loop and break out when condition doesn't meet.
Code:
use strict; |
@ Mike: I see now that your original solution didn't have the problem I thought it did.
Code:
sub findOldFile {Here's an example of what I mean: Code:
#!/usr/bin/env perlAll of that said, Ghostdog's suggestion is good too. The Filesys::Df module looks pretty good. (No cap on the 's' of 'sys', by the way.) |
Thanks for the help guys, I'll be playing with this code today. . . Hopefully with all the help you guys have given me, I'll get it going.
|
| All times are GMT -5. The time now is 07:10 AM. |