#!/bin/sh
#
# 18/5/2018
#
echo "this shell script will watch when a process ends"
if [ $# -ne 1 ] ; then
echo "Usage: processwatcher process-directory"
echo " For example: processwatcher /proc/14856 where 14856 is the PID of the target process"
exit 255
fi
FN=$1
# process directory i.e. /proc/14856
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 processwatcher script"
exit 0
I have given the name of this script as processwatcher.sh,suppose another job or process is going on simultaneously and i want to know when it ends ,my script will serve that purpose when we run it with the Process ID of the target process or job as an argument to this shell script
For example we created this shell script to show the usage of break and continue in shell scripting ,it will run for a certain amount of time
#!/bin/sh
#
# 18/5/2018
#
echo "Break Continue Example"
FN1=/tmp/break.txt
FN2=/tmp/continue.txt
x=1
while [ $x -le 1000000 ]
do
echo "x:$x"
if [ -f $FN1 ] ; then
echo "Running the break command"
rm -f $FN1
break
fi
if [ -f $FN2 ] ; then
echo "Running the continue command"
rm -f $FN2
continue
fi
let x++
sleep 2
done
echo "x:$x"
echo "End"
exit 0
we first run it from the terminal
sh ./breakandcontunue.sh
Now to see what process ID it has while running we need to use the grep command
ps auxw | grep break* | grep -v grep
the last piped section was to filter the grep search which itself acts as a process
the output is like this
sourav 14856 0.0 0.0 12876 2996 pts/1 S+ 20:05 0:00 sh ./breakandcontunue.sh
So the PID as we can see is 14856
so in the /proc/folder we should find a folder named 14856 and it will continue to exist while this script/process/job runs
our shell script processwatcher is basically checking the existence of this folder and thus if the process is running with an interval of 1 second with the help of sleep command
#
# 18/5/2018
#
echo "this shell script will watch when a process ends"
if [ $# -ne 1 ] ; then
echo "Usage: processwatcher process-directory"
echo " For example: processwatcher /proc/14856 where 14856 is the PID of the target process"
exit 255
fi
FN=$1
# process directory i.e. /proc/14856
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 processwatcher script"
exit 0
I have given the name of this script as processwatcher.sh,suppose another job or process is going on simultaneously and i want to know when it ends ,my script will serve that purpose when we run it with the Process ID of the target process or job as an argument to this shell script
For example we created this shell script to show the usage of break and continue in shell scripting ,it will run for a certain amount of time
#!/bin/sh
#
# 18/5/2018
#
echo "Break Continue Example"
FN1=/tmp/break.txt
FN2=/tmp/continue.txt
x=1
while [ $x -le 1000000 ]
do
echo "x:$x"
if [ -f $FN1 ] ; then
echo "Running the break command"
rm -f $FN1
break
fi
if [ -f $FN2 ] ; then
echo "Running the continue command"
rm -f $FN2
continue
fi
let x++
sleep 2
done
echo "x:$x"
echo "End"
exit 0
we first run it from the terminal
sh ./breakandcontunue.sh
Now to see what process ID it has while running we need to use the grep command
ps auxw | grep break* | grep -v grep
the last piped section was to filter the grep search which itself acts as a process
the output is like this
sourav 14856 0.0 0.0 12876 2996 pts/1 S+ 20:05 0:00 sh ./breakandcontunue.sh
So the PID as we can see is 14856
so in the /proc/folder we should find a folder named 14856 and it will continue to exist while this script/process/job runs
our shell script processwatcher is basically checking the existence of this folder and thus if the process is running with an interval of 1 second with the help of sleep command
No comments:
Post a Comment