1: In bash, $!
holds the PID of the last background process that was executed. That will tell you what process to monitor, anyway.
4: wait <n>
waits until the process with ID is complete (it will block until the process completes, so you might not want to call this until you are sure the process is done). After wait
returns, the exit code of the process is returned in the variable $?
2, 3: ps
or ps | grep " $! "
can tell you whether the process is still running. It is up to you how to understand the output and decide how close it is to finishing. (ps | grep
isn't idiot-proof. If you have time you can come up with a more robust way to tell whether the process is still running).
Here's a skeleton script:
# simulate a long process that will have an identifiable exit code
(sleep 15 ; /bin/false) &
my_pid=$!
while ps | grep " $my_pid " # might also need | grep -v grep here
do
echo $my_pid is still in the ps output. Must still be running.
sleep 3
done
echo Oh, it looks like the process is done.
wait $my_pid
my_status=$?
echo The exit status of the process was $my_status