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 biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Simple unix script.. 1

Status
Not open for further replies.

galger

MIS
Jan 16, 2002
79
US
I know this is simple...


Need a unix script to check if a specific process is running
every 300 second.
If it finds it, email someone....
then exit..

 
Not particularly simple and it's been answered many times before on this forum.

The standard way to approach it is to run a ps with options,
pipe the output through grep, filter this for the grep
process and report the OP.

A while loop and sleep fulfill your other requirement.

 
procname is the name of the process you are looking to find.

cron version: (put in cron to run every 10 minutes)
#!/bin/ksh
#
PID=`ps -elf | grep procname | awk 'print { $4 }'`
if [ -z "$PID" ] ; then
print ""procname not found"
else
print "procname is running"
/maildirectory/mail -s "procname is running" youremail@yourcompany.com
fi
exit 0

Non-cron version:
#!/bin/ksh
#
PID=" " # initialization
DONE="FALSE"
while [ $DONE = "FALSE" ]
do
PID=`ps -elf | grep procname | awk 'print { $4 }'`
if [ -z "$PID" ] ; then
print "procname not found"
else
print "procname is running"
/maildirectory/mail -s "procname is running" youremail@yourcompany.com
fi
sleep 300
done

May contain minor syntax issues as I am not where I can test it. It is also possible to do this without awk if your version of grep can run silent. If so, grep will set a return code if the value is found.
 
Thanks gamerland!!!

That worked, I changed a few things though... the grep part and the if statement. added an exit 1 to exit after emailing.

#!/bin/ksh
#
PROCESS=" " # init alias
DONE="FALSE"
while [ $DONE = "FALSE" ]
do
PROCESS=`ps -elf | grep -c ex_part1`
if [ "$PROCESS" -eq 1 ] ; then
print "proc not found" >/dev/null
else
print "proc is running" >/tmp/foo.txt
mailx -s &quot;The process has been detected!&quot; mail@mail.com </tmp/foo.txt
mailx -s &quot;The process has been detected!&quot; mail@mail.com< /tmp/foo.txt
exit 1
fi
echo &quot;sleeping......&quot;
sleep 600
done
------------------------------------------


Thanks again!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top