Recently, we asked our readers to share some of their Linux scripts with us. Our first script comes to us from Diethard Ohrt, who sent us a script named “survf”. He writes:
The script “survf” monitors a file so you can check whether this file is growing (e.g. during ftp transfer). If you link it to the name “survp,” it monitors a running process… when the process terminates it sounds a bell and terminates.
Take a look at survf and give it a try. Diethard adds that he originally wrote it for the Korn shell on a Unix box a few years ago (so you might want to tweak it with “proper, real bash syntax.”)
Thank you, Diethard! To show our appreciation, we are sending you a gift certificate for some Starbucks coffee. Enjoy.
Let us know what you think of the script or send us one of your own. If we use it, you can earn yourself a Starbucks gift certificate plus you’ll be helping out other users.
If you would like some more scripts, check out our tips section. Whether it is help with Linux migrations or managing high-volume CPU processes, our SearchEnterpriseLinux experts help you navigate through the Linux world.
Hope you like the script. Keep them coming.
!/bin/bash
survp/f: primitive process/file surveillance
==================================================
monitors a given process using ps(1)
process may be given by PID or name
if called as "survf", a given file is monitored
("CUP" means "cursor up" ...)
__________________________________________________
PROGNAME=`basename $0`
trap echo -e "\n$PROGNAME: terminated." exit 0 2 15
is_int=0
How have we been called? _________________________
if [ $PROGNAME = survp ]
then
OBJECT=process
CMD="ps -U $LOGNAME | grep $1"
if (( $ != 1 ))
then
echo "usage: $PROGNAME { pid | process_name }"
exit 1
fi
Check: is parameter a number, thus PID?
export item2test=$1
bash -u -c typeset -i NUM=$item2test > /dev/null 2>&1
(( $? == 0 )) && is_int=1
else
invoked as "survf" _____________________________
OBJECT=file
CMD="ls -l $1"
if (( $ != 1 ))
then
echo "usage: $PROGNAME { file_name }"
exit 1
fi
fi
typeset -i STATE=0
echo $PROGNAME: surveillance of $OBJECT $1
echo " (use ^C to terminate)"
CUP=`tput cuu1``tput cuu1`
while [ true ]
do
if [ $OBJECT = process ]
then
if (( $is_int == 0 ))
then
ps -u $LOGNAME | grep $1
STATE=$?
else
ps -fp $item2test
STATE=$?
echo $CUP
fi
else
$CMD
STATE=$?
fi
if (( $STATE != 0 ))
then
echo -e "07\n$PROGNAME: *** ERROR *** $OBJECT $1 not found!"
exit 1
fi
echo $CUP
sleep 10
done