Skip to content

Instantly share code, notes, and snippets.

@peterbe peterbe/run-forever.sh
Last active Feb 15, 2018

Embed
What would you like to do?
#!/usr/bin/env bash
set -eo pipefail
count=0
_stopnow() {
count="$(($count+1))"
test -f stopnow && \
echo "Stopping after $count iterations!" && \
rm stopnow && exit 0 || return 0
}
control_c()
# run if user hits control-c
{
echo "Managed to do $count iterations"
exit $?
}
# trap keyboard interrupt (control-c)
trap control_c SIGINT
echo "To stop this forever loop created a file called stopnow."
echo "E.g: touch stopnow"
echo ""
echo "Now going to run '$@' forever"
echo ""
while true
do
_stopnow
eval $@
# Do this in case you accidentally pass an argument
# that finishes too quickly.
sleep 1
done
@lamby

This comment has been minimized.

Copy link

commented Feb 13, 2018

This should be:

"$@"

.. by itself on a newline (without the eval) otherwise you expand spaces. Try, for example, running ls "foo bar" in a loop. With a raw eval $@ you would see "cannot open foo, cannot open bar` which is incorrect

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.