There are a few more topics that we should look at in this chapter. Suppose you want to be alerted when a running process ends on your system.
Here's a script that notifies the user when the specified process ends. Note that there are other ways to do this task, this is just one approach.
#!/bin/sh # # 5/3/2017 # echo "script12 - Linux Scripting Book" if [ $# -ne 1 ] ; then echo "Usage: script12 process-directory" echo " For example: script12 /proc/20686" exit 255 fi FN=$1 # process directory i.e. /proc/20686 rc=1 while [ $rc -eq 1 ] do if [ ! -d $FN ] ; then # if directory is not there echo "Process $FN is not running or has been terminated." let rc=0 else sleep 1 fi done echo "End of script12" exit 0
To see this script in action run the following commands:
In a terminal, run
script9
In another terminal run
ps auxw | grep script9
. The output will be something like this:guest1 20686 0.0 0.0 106112 1260 pts...