You can't have a pipe inside [ ... ]
. It's also better to use pgrep
than to try to parse the output of ps
:
count=0
while [ "$count" -lt 10 ] && pgrep relayevent.sh >/dev/null; then
sleep 3
count=$(( count + 1 ))
done
BSD systems could use pgrep -q ...
instead of pgrep ... >/dev/null
to discard the actual output of pgrep
, just as with ordinary grep
(we're only interested in the exit status).
Note how we don't put the pgrep
command within [ ... ]
. That's because we're not interested in its output, only its exit status. With [ ... ]
you commonly compare strings or numbers. The [ ... ]
will result in an exit status that is zero (true) or non-zero (false), just like the pgrep
execution.
However, this does not check for any locking mechanisms, only whether a particular process is running or not.
If you are trying to get only a single instance of a script running, then it's better to do something like this (assuming the EXIT
trap is executed whenever the script is terminating orderly):
lockdir=dir.lock
if mkdir "$lockdir"; then
trap 'rmdir "$lockdir"' EXIT
else
echo 'Only one instance of this script allowed' >&2
exit 1
fi
With a number of tries and sleep:
lockdir=dir.lock
count=0
while [ "$count" -lt 10 ]; then
if mkdir "$lockdir"; then
trap 'rmdir "$lockdir"' EXIT
break
else
echo 'Locked. Sleeping...' >&2
sleep 3
fi
count=$(( count + 1 ))
done
if [ "$count" -eq 10 ]; then
echo 'Giving up.' >&2
exit 1
fi
Related:
flock
command to implement locks in scripts.