Use case: You’re writing a shell script to stop/start a system service, but only want to do so when there is no activity in the server log. Change the FILE variable below to point to that log file, and modify the actions below under DONE=1 to what is needed.

Of course this assumes there is a file called “test-file” in the same directory as the script, but that can be changed easily. The file could also be modified to accept parameters on how long to wait, but that’s left up to the reader if the functionality is required.

#!/bin/bash
FILE=test-file
if [ ! -f ${FILE} ]; then
	echo 'file dont be there'
	exit 1
fi
DONE=0
CHANGED=0
while [ "$DONE" != "1" ]; do
	THEN=`tail -1 $FILE`
	for i in `seq 1 10`; do
		NOW=`tail -1 $FILE`
		if [ "$NOW" == "$THEN" ]; then
			CHANGED="0"
			# DEBUGGING # echo 'no change'
		else
			CHANGED="1"
			break;
		fi
		sleep 1
	done
	# DEBUGGING # set -x
	if [ "$CHANGED" == "0" ]; then
		DONE="1"
	fi
	# DEBUGGING # echo "Done is $DONE"
	# DEBUGGING # echo "Changed is $CHANGED"
	sleep 1
	# DEBUGGING # set +x
done