Beefy Boxes and Bandwidth Generously Provided by pair Networks RobOMonk
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Unlink works in windows, not in Linux?

by jagexCoder (Novice)
on Dec 12, 2012 at 00:31 UTC ( #1008432=perlquestion: print w/ replies, xml ) Need Help??
jagexCoder has asked for the wisdom of the Perl Monks concerning the following question:

Hi I have a server that creates log files of with certain file names, I'm use regex to detect these filenames and based on them delete the logs that are older than 24 hours. The script works great tested on Windows XP, however in Linux it does not work. The backup files are not deleted and the log does not mention any files were deleted. Also the $unlink does not display the warning message. Here's the relevant part of the code:
until ($log_written != 0) { print FILE "*** SCRIPT RUN ON: " . $dt . " ***\n"; print FILE "Deleting backup files more than 1 day old:\n"; foreach my $file (<grm_backup-*_*_*_*_*_*_EST_*.bak>) { if ( -M $file > 2 ) { #print "Deleting the log file more than 1 days old: " . $f +ile; unlink $file or die "\nCould not delete the backup $file: +$!"; print FILE "Deleted -> " . $file . "\n"; } } foreach my $file2 (<backup_db-*_*_*_*_*_*_EST_*.bak>){ if ( -M $file2 > 2 ) { #print "Deleting the log file more than 1 days old: " . $f +ile2; unlink $file2 or die "\nCould not delete the backup $file2 +: $!"; print FILE "Deleted -> " . $file2 . "\n"; } } print FILE "\n\n"; print "Backups older than 1 day have been successfully deleted! Se +e LOG file for more details.\n"; $log_written = 1; }
All advice appreciated thanks EDIT: I should mention that unlink works if I type in: "unlink cmc_rules-Mon_Dec_12_12_31_01_EST_2012" Just not via the script

Comment on Unlink works in windows, not in Linux?
Download Code
Re: Unlink works in windows, not in Linux?
by Athanasius (Prior) on Dec 12, 2012 at 04:03 UTC

    I don’t know of any reason why unlink should fail under Linux, and in any case, from the fact that there is no error message (i.e., die is not called), it seems unlikely that unlink is the problem. Try running something like this:

    for ( <grm_backup-*_*_*_*_*_*_EST_*.bak>, <backup_db-*_*_*_*_*_*_EST_*.bak> ) { printf "File '%s' was last modified %d days ago\n", $_, -M; }

    to verify that there are files meeting the criterion for deletion.

    Note also that the until loop is doing nothing. It is equivalent to:

    while ($log_written == 0) { ... $log_written = 1; }

    which guarantees that there will only ever be a single pass.

    Hope that helps,

    Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

Re: Unlink works in windows, not in Linux?
by eyepopslikeamosquito (Canon) on Dec 12, 2012 at 04:53 UTC
Re: Unlink works in windows, not in Linux?
by tobyink (Abbot) on Dec 12, 2012 at 07:07 UTC

    Does the user running the Perl script have write permission to the files in question, and their directory?

    perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'
Re: Unlink works in windows, not in Linux?
by rovf (Priest) on Dec 12, 2012 at 10:00 UTC
    Honestly, I don't think that the file is not deleted, but you still don't the error message. I suggest you replace

    unlink $file or die "\nCould not delete the backup $file: $!";
    by
    print FILE "Now going to delete $file\n"; if(unlink $file) { print FILE "$file has been removed (says 'unlink')\n"; if(-e $file) { print FILE "Surprise, surprise! $file is still here\n"; } } else { print FILE "'unlink' says: $!\n"; if(-e $file) { print FILE "And indeed, we still see $file\n"; } else { print FILE "A miracle occured! $file is gone!\n"; } }

    -- 
    Ronald Fischer <ynnor@mm.st>
Re: Unlink works in windows, not in Linux?
by Anonymous Monk on Dec 12, 2012 at 17:05 UTC
    unlink $file or die Fudge("unlink q($file)"); sub Fudge { use Errno(); join qq/\n/, "Error @_", map { " $_" } int( $! ) . q/ / . $!, int( $^E ) . q/ / . $^E, grep( { $!{$_} } keys %! ), q/ /; }

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1008432]
Approved by LanX
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (15)
As of 2014-04-16 20:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (433 votes), past polls