Skip to main content
Tweeted twitter.com/StackUnix/status/754865428372004869
edited tags; edited title
Link
Jeff Schaller
  • 68.8k
  • 35
  • 122
  • 264

Bash Shell script: "if file is not being used" condition

Source Link
Brady Dean
  • 245
  • 2
  • 3
  • 8

Bash script "if file is not being used" condition

I'm trying to compress a virtual machine image file through a script, but I want to be sure the file isn't being accessed. I could check if virt-manager is running, since it should be the only program accessing the image, but I don't know if there's a better way to do it. I also want the script to continue trying until the file is available to compress. I don't know how do to that either.

#Check if virt-manager is running
if pgrep "virt-manager" > /dev/null
then
    #re-run script until success
else
    gzip -k < /home/brady/.vms/windows10/hdd.img > /media/backup/vms/windows10/hdd.$(date +"%F.%T).img.gz