Hi Unix Members,
Can anyone guide me to write one shell script to monitor the attach screen processes and when interrupted mail us. ,
like the processes - /bin/ciserver , /bin/clock , /bin/cserver , /bin/main
Please looking forward you guys help.
Hi Unix Members,
Can anyone guide me to write one shell script to monitor the attach screen processes and when interrupted mail us. ,
like the processes - /bin/ciserver , /bin/clock , /bin/cserver , /bin/main
Please looking forward you guys help.
Any ideas/attempts from your side?
At a (very) high level, you want:
while true
do
for processname in $ListOfProcessesToLookFor
do
if processnotrunning $processname
then
echo "Oh dear! The process $processname appears to have stopped! This message was brought to you by the letter C at `date`" | SendMeAnEmail $MyEmailAddress "Warning: Process $processname is not running!"
fi
done
sleep $TimeToWaitBetweenChecks
done
Hi Unix Gurus,
I want a very simple script. Like
Ps -ef|grep /home/unispool/bin/main
and then check if the process is running or not.
If you want a simple script, then it will only be able to achieve simple things.
uname -a
would be great.Sorry for all the questions.
Robin
Hi Robin,
Thanks for the questions . I am new in the shell scripting.
So the processes I need to monitor. Like when the process get stopped unexpectedly , I need an alert. The Simple script like ,
Will this code work?? like or something more can i do ?
#!/bin/ksh
while true ; do
isRunning=`ps -ef | grep -v grep | grep /home/unispool/bin/main`
if [ $isRunning -eq 3 ]
then
echo "running"
else
echo "not running"
mailx -s "Main process not running correctly" biswajit.nitd@yahoo.in < /dev/null
fi
isRunning1=`ps -ef | grep -v grep | grep /home/unispool/bin/cserver`
if [ $isRunning1 -eq 4 ]
then
echo "running"
else
echo "not running"
mailx -s "Cserver process not running correctly" biswajit.nitd@yahoo.in < /dev/null
fi
isRunning2=`ps -ef | grep -v grep | grep /home/unispool/bin/ciserver`
if [ $isRunning2 -eq 3 ]
then
echo "running"
else
echo "not running"
mailx -s "ciserver process not running correctly" biswajit.nitd@yahoo.in < /dev/null
fi
done
I fail to see how:
isRunning=`ps -ef | grep -v grep | grep /home/unispool/bin/main`
(which sets the variable isRunning
to the lines reported by ps
that contain the string /home/unispool/bin/main
(not the number of line that contain that string; the actual lines themselves) would ever be a string that is a numeric string equal in value to the number 3. The same thing can be sad for the other two ps
pipelines. So, this script is logically equivalent to:
#!/bin/ksh
while true ; do
echo "not running"
mailx -s "Main process not running correctly" biswajit.nitd@yahoo.in < /dev/null
echo "not running"
mailx -s "Cserver process not running correctly" biswajit.nitd@yahoo.in < /dev/null
echo "not running"
mailx -s "ciserver process not running correctly" biswajit.nitd@yahoo.in < /dev/null
done
which will mostly just fill your mailbox with meaningless messages as fast as your system can run ps
, grep
twice, and mailx
.
If you're trying to count the number of lines produced by ps
as filtered by grep
in those pipelines, you need to either add a wc -l
to the end of the pipeline or add a -c
option to the last grep
command in the pipeline. (I strongly prefer the latter option since it uses fewer system resources and will run faster.)
But, I fail to see how draining system resources with a continuous loop to slow down your system when it is working well (and to further slow it down and fill up your mailbox rapidly when something isn't running as expected) is a good idea.
Have you considered putting a sleep in your loop (so your system can perform some meaningful work in addition to looking for an imbalance in the three things you seem to think are the only things that matter on your system)?
Have you considered exiting your loop once mail has been sent so you can find one message in your mailbox, fix the problem, and then restart the loop to wait for the next occurrence of a problem?