0

So i'm working on a script to remove some old backups when the disk space available goes bellow a certain percentage.

    # get disk info
    $handle = Filesys::DiskFree->new();
    $handle->df();
    $available = $handle->avail("$dir");
    $used = $handle->used("$dir");
    $total = $handle->total("$dir");
    $used_p = ($used / $total) * 100.0;
    print "$used_p\n";
    print "$used\n";

    # find files to delete
    # --- special sauce code to find @files

    # delete files
    unlink @files;

    # get new disk info
    $handle = Filesys::DiskFree->new();
    $handle->df();
    $available = $handle->avail("$dir");
    $used = $handle->used("$dir");
    $used_p = ($used / $total) * 100.0;
    print "$used_p\n";
    print "$used\n";

My thinking was that the second time i used Filesys::DiskFree I would get the new df values which should reflect the files deleted. However, is not giving me the values i expect. Is not exactly the same, but the difference is not nearly as much as I expect. However the second time i run it I can see that the initial print reflects the files being deleted on the previous run. It is as if there was some sort of delay. As shown above, I tried calling new() again to see if that would clear stale data.

Lex
  • 386
  • 1
  • 4
  • 20
  • 1
    it's possible that the system isn't releasing the diskspace until the application terminates. Much like a long running application that generates large log files, deleting the file removes it, but the space isn't freed until the application is reloaded. Try an `lsof | grep deleted`... you may see your files still listed, and if so and they're gone after the app terminates, this is the issue. – stevieb Nov 30 '16 at 23:22
  • This was my issue. Thanks. – Lex Dec 01 '16 at 20:44
  • I made it an answer so that it can be viewed by future readers with this problem. – stevieb Dec 01 '16 at 21:01

1 Answers1

1

It's possible that the system isn't releasing the disk space until the application terminates. Much like a long running application that generates large log files, deleting the file removes it, but the space isn't freed until the application is reloaded. Try an lsof | grep deleted... you may see your files still listed, and if so and they're gone after the app terminates, this is the issue.

stevieb
  • 9,065
  • 3
  • 26
  • 36