Skip to content

Instantly share code, notes, and snippets.

@saravanabalagi
Last active September 25, 2023 19:36
Show Gist options
  • Select an option

  • Save saravanabalagi/66893b659505cd4e142ca3bc65fd6978 to your computer and use it in GitHub Desktop.

Select an option

Save saravanabalagi/66893b659505cd4e142ca3bc65fd6978 to your computer and use it in GitHub Desktop.
Executes command after a process completes
#!/bin/bash
if [ $1 == '--help' ] || [ $1 == 'help' ] || [ $1 == '-h' ] ; then
echo "usage: runafter <pid> [-f] <command>"
exit 0
fi
re='^[0-9]+$'
if ! [[ $1 =~ $re ]] ; then
echo "error: first arg must be a pid of a running process" >&2
exit 1
fi
pid=$1
start=2
if [ $2 == '-f' ] ; then
start=3
fi
if ! [ -n "$pid" -a -e /proc/$pid ]; then
if [ $2 != '-f' ]; then
echo "error: process $pid is not running"
echo "add -f after pid to force run even when a process is stopped"
exit 1
fi
fi
echo "Waiting for process $pid to finish..."
tail --pid=$pid -f /dev/null
printf "Process $pid finished, running command:\n\t${@:$start}"
"${@:$start}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment