0

I need to remove the contents of a directory D based on the below condition

  1. Get the used space of directory D.
  2. If the used space is above the threshold, then remove the contents of directory based on last modified time (using find mtime)

Have already written a shell script for it (clearSpace.sh), but the problem is that the script can be called by multiple processes simultaneously.

I want the steps 1 & 2 to be atomic so that I can get consistent results.

Is there a way where I first get a "lock" on directory D and execute clearSpace.sh and then give the lock? Permission based locking is not an option

James Brown
  • 36,089
  • 7
  • 43
  • 59
Mohit K.
  • 71
  • 8

1 Answers1

0

We suggest refrain from locking storage or any other OS resources. The side effects could be devastating. And unrelated.

The responsibility of the OS is to sync, distribute and manage resources.

Resource locking is kernel level programming. Better not get there. There is a reason for not having locking features in the OS scripting tools.

You can implement script access locking. Using a lock.id file to signal that the script is running.

Forbid the script to run if lock.id exist.

And remove the lock.id file when the script operation is completed. We suggest you change your script thresholds to be more flexible and tolerant.

Dudi Boy
  • 4,551
  • 1
  • 15
  • 30