A while ago I have written a script to wait for the end of another process. NOISE_CMD
could be something like notify-send ...
, given that DISPLAY
is set correctly.
#!/bin/bash
NOISE_CMD="/usr/bin/mplayer -really-quiet $HOME/sfx/alarm-clock.mp3"
function usage() {
echo "Usage: $(basename "$0") [ PROCESS_NAME [ PGREP_ARGS... ] ]"
echo "Helpful arguments to pgrep are: -f -n -o -x"
}
if [ "$#" -gt 0 ] ; then
PATTERN="$1"
shift
PIDS="$(pgrep "$PATTERN" "$@")"
if [ -z "$PIDS" ] ; then
echo "No process matching pattern \"$PATTERN\" found."
exit
fi
echo "Waiting for:"
pgrep -l "$PATTERN" "$@"
for PID in $PIDS ; do
while [ -d "/proc/$PID" ] ; do
sleep 1
done
done
fi
exec $NOISE_CMD
Without any argment, just make some noise immediately. This behavoir allows something like this for convenience (say you call script below alarm.sh
):
apt-get upgrade ; ~/bin/alarm.sh
Of course you can do many funny things with such a script, like letting an instance of alarm.sh
wait for an instance of alarm.sh
, that is waiting for some other command. Or executing a command just right after some task of a other logged in user has finished... >:D
A former version of the script above might be interesting, if you want to avoid a dependency on pgrep
and accept to lookup process IDs yourself:
#!/bin/bash
if [ "$#" -lt 2 -o ! -d "/proc/$1" ] ; then
echo "Usage: $(basename "$0") PID COMMAND [ ARGUMENTS... ]"
exit
fi
while [ -d "/proc/$1" ] ; do
sleep 1
done
shift
exec "$@"
Slightly off-topic, but useful in similar situations: One might be interested in reptyr
, a tool that "steals" a process from some (parent) shell and runs it in from current shell. I have tried similar implementations and reptyr
is the prettiest and most reliable for my purposes.