Skip to content

Instantly share code, notes, and snippets.

@EvgenyOrekhov
Last active May 17, 2020 08:36
Show Gist options
  • Save EvgenyOrekhov/c1c390e088fe7beb4542a91c5cca8956 to your computer and use it in GitHub Desktop.
Save EvgenyOrekhov/c1c390e088fe7beb4542a91c5cca8956 to your computer and use it in GitHub Desktop.
work-n-rest.sh - a POSIX shell script for executing long running tasks with pauses

work-n-rest.sh

A POSIX shell script for executing long running tasks with pauses.

Usage examples

Work for 10 seconds, then rest for 10 seconds:

./work-n-rest.sh find ./ -name '*.log'

Work for 20 seconds, then rest for 5 seconds:

WORK=20 REST=5 ./work-n-rest.sh find ./ -name '*.log'
#!/bin/sh
set -eu
EX_USAGE=64
if [ $# -eq 0 ]; then
printf 'Error: missing argument\nUsage: %s COMMAND\n' "$(basename "$0")" >&2
exit $EX_USAGE
fi
WORK=${WORK:-10}
REST=${REST:-10}
eval "$* &"
PID=$!
killOrExit() {
if ps --pid "$PID" > /dev/null; then
kill -s "$1" "$PID"
else
wait $PID
exit $?
fi
}
while true; do
sleep "$WORK"
killOrExit STOP
sleep "$REST"
killOrExit CONT
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment