Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

assistance required

Status
Not open for further replies.

theydon

Technical User
Dec 23, 2002
1
CH
I am new to scripting and have been tasked with the following:-

Using Boune shell, write a program which will loop periodically untill told to do otherwise.

The script must periodically check if a process or processes have died and restart it. If the process fails to start after 3 attempts due to it core dumping the program should exit to allow for manual investigation.

I would like this script to be called from within anouther start script, which may not have finished executing itself

p.s
Can someone recommend some good bourne shell scripting books which would aid in me learning . I really do need to get to grips with this urgently.

Thanks in advance
 
This is an example that needs work on
the process detection section. The
word matches with grep and
the $pname need to be reworked and foolproofed.

It is intended as a model and
not for actual use!
Your corefile detection is entirely up to you.

As an effective way to start
scripting in bash in a hurry I
suggest "Linux and Unix shell scripting"
by D.Tansley from Addison-Wesley.
It's a good hands on basics book,
though some important ideas
(like arrays) are noticeably missing.
There are excellent tutorials and howtos on the web as well.


#!/bin/sh

trap "myexit" 2 3 15

if [ "$EUID" != "0" ] ; then
echo "Fatal: You must run this script as the root user"
exit 1 > /dev/null
fi
#globals
errfile="/tmp/error.$0"
touch $errfile
errcount=`cat $errfile`
test -z $errcount && errcount=0
dumpfile="/tmp/dump.$0"
touch $dumpfile



function checkProc() {
pname=${1:-"null"}
if [ "$pname" != "null" ] ; then
idtag=`ps -aux | grep $pname | awk ' {print $11}'`
if [ "$idtag" = "grep" ] ; then
errcount=`expr $errcount + 1`
echo $errcount > $errfile
restart_ $pname
test $errcount -gt 2 && echo "Fatal: program not responding" && exit 1 > /dev/null
sleep 5
checkProc $pname
else
pstring=`echo $idtag | sed "/$pname/!d"`
if [ ! -z "$pstring" ] ; then
echo $errcount > $errfile
return 0
else
echo "fatal: could not derive $pname from filtered output"
echo $pstring > $dumpfile
exit 1 > /dev/null
fi
fi
else
echo "fatal: no process name given to function checkproc()"
exit 1 > /dev/null
fi
return 1
}

function restart_() {
prog=$1
exec $prog 2>&1 $dumpfile
}

function myexit() {
echo $errcount > $errfile
echo "Closing scriptname $0, on sigaction"
exit 12 > /dev/null
}

while :
do
checkProc $1
sleep 600
done
 
In Korn Shell functions have a syntax of:

function MyFunction
{
}

OR

MyFunction()
{
}
 
This is a C shell script that will do the job, cant get much simpler than this. Just play with it a little to work in Bash (probably only need to change /bin/csh to #!/bin/sh)

Then run a cron to call it every minute and Bob's your uncle:

#!/bin/csh
foreach DAEMON ( MonitorSuLog.pl MonitorLogins.pl DiskHogs.pl )
ps -e | fgrep "$DAEMON:t" | cut -c1-8 > /dev/null
if ( $status > 0 ) then
echo "Restarting $daemon"
date
$DAEMON &
endif
end

I would be inclined to add a logging or mail routieen so I knew when it had been doing a restart, probably something like:

~~~~~~
Started=`date`
~~~~~
if ( $status > 0 ) then
echo "Restarting $DAEMON on $Started >> /some/restart/log.txt
echo "---------------" >> /some/restart/log.txt
/usr/ucb/mail -s &quot;Restarting $DAEMON&quot; you@your<dot>net < /some/restart/log.txt
$DAEMON &
endif

Good luck
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top