Pause shell script till folder doesn't change size anymore

Hi,

I recently would like to write a shell script that

  1. Runs in the background (can be done with "&", but i'd be happy for other solutions to keep programs running)
  2. Does its stuff
    3 THEN checks a specified folder for a size change over time (say, each 5 seconds. AND ONLY continues with the next line in the script when said folders sizes doesn't change anymore.

I hope you got any good suggestions for me.

Thanks in advance

Try this:

#!/bin/sh

DIR=~/'unix.com'
dir_size=0;

get_size(){ du -s "$1" 2>/dev/null | awk '$0=$1';}

while [ : ]; do
	# Script does its stuff
	sleep 5
	while [ "$(get_size "$DIR")" -ne "$dir_size" ]; do
		dir_size=$(get_size "$DIR")
		sleep 5
	done
	echo "Size has not changed since the last 5 seconds. Loop to the top"
done

exit 0

Save this as 'script.sh' or something, and run it with ./script.sh &

1 Like

If I add something between the exit 0 and done, will that be executed after yhe size check has finished ?
Also, I'd prefer to have "the stuff that has to be done by the script" to be only executed once, and before that loop even starts, then loop till the sizechange isn't happenig anymore and after that continue with whatever I might still want.

Could this possibly be done in more compact form (one-liner lol ?)

Thanks for your answer.

#!/bin/sh

DIR=~/'unix.com'

get_size(){ du -s "$1" 2>/dev/null | awk '$0=$1';sleep 5;}

# Script does its stuff

dir_size=$(get_size "$DIR")
while [ "$(get_size "$DIR")" -ne "$dir_size" ]; do
	echo 'Size has changed. Sleeping for 5 more seconds'
	dir_size=$(get_size "$DIR")
done

echo 'Size has not changed since the last 5 seconds.'

#Do other stuff

exit 0
1 Like

And the "script does it's stuf will ONLY be executed once, right :slight_smile: ?

Yes, # Script does its stuff and # Do other stuff will be executed only once.

Perfect :slight_smile: Thanks !

Will try that out ASAP !

---------- Post updated at 04:21 PM ---------- Previous update was at 02:44 PM ----------

And the path is changed by simply lets say, replacing this:

with

for example ?

Yes, unless, for example, you're talking about /var/mobile in the system root, in which case you'll issue DIR='/var/mobile' , as ~ denotes your Home directory

1 Like

I get line 5 error integer expression expected

I put the line with the dir like this:

DIR='/var/mobile/Media/"My Music"/'

Also, would it be possible to write the script without the "dostuff" and the "do other stuff ?

I only want this scriptpart to stop the script (withthat Loop you provided) Till the Folder Sizes remains the Same (e.g.doesn't change anymore).

EDIT:

Nevermind, if I use /var/mobile/Media/ as the Dir it works beutifully :slight_smile: (seems that "My Music" makes problems...)

Thanks for this, its just what I needed :wink:

DIR='/var/mobile/Media/My Music/' will do.

You can write this script in a new script, of course:
(Call it for this example "pause.sh")

#!/bin/sh

get_size(){ du -s "$1" 2>/dev/null | awk '$0=$1';sleep 5;}

dir_size=$(get_size "$1")
while [ "$(get_size "$1")" -ne "$dir_size" ]; do
	echo 'Size has changed. Pausing 5 more seconds'
	dir_size=$(get_size "$1")
done

echo 'Size has not changed since the last 5 seconds.'

return 0

In your script, do the following:

#!/bin/sh

DIR=~/'unix.com'

echo "Do stuff"

./pause.sh "$DIR"

echo "Do other stuff"

exit 0
1 Like