HPUX Scripting

I am writing a script to check if the snmpd service is running on a dev server. I have the script written but every time I run it the results come back that the service is running even if I have manually stopped the service. I am very confused with this. The script is below.

#!/bin/bash

OUTPUT=$(ps -ef | grep snmpd | wc -l)

if [ $OUTPUT -lt 1 ];
then
        echo "SNMPD is not running"
else
        echo "SNMPD is running"
fi
Frog_1337Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

woolmilkporcCommented:
The problem is that grep will in most cases find its own command line in the ps list,
so the result will then be (at least) "1", even when the concerned process is not found.

There are two solutions I'm aware of:

#!/bin/bash
OUTPUT=$(ps -ef | grep snmpd | grep -v grep | wc -l)
if [ $OUTPUT -lt 1 ];
then
        echo "SNMPD is not running"
else
        echo "SNMPD is running"
fi

or

#!/bin/bash
OUTPUT=$(ps -ef | grep snmp[d] | wc -l)
if [ $OUTPUT -lt 1 ];
then
        echo "SNMPD is not running"
else
        echo "SNMPD is running"
fi

A shorter solution:

#!/bin/bash
if ps -ef | grep -q snmp[d]; then
        echo "SNMPD is running"
else
        echo "SNMPD is not running"
fi

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Frog_1337Author Commented:
That was it thank you so much
woolmilkporcCommented:
You're always welcome, glad I could help!

Please consider accepting my comment as the solution.

Greetings

wmp
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Shell Scripting

From novice to tech pro — start learning today.