Shell scripting

Hi,

I have this piece of a shell script:

# Initialization:
: ${__OCF_ACTION=$1}
OCF_SUCCESS=0
OCF_ERROR=1
OCF_STOPPED=7

agent_monitor() {
        # Monitor _MUST!_ differentiate correctly between running
        # (SUCCESS), failed (ERROR) or _cleanly_ stopped (NOT RUNNING).
        # That is THREE states, not just yes/no.

                file='/var/opt/dpa/pidfiles/aaaaa_AAAAA.pid'
                if [ -f $file ]; then
                        check1=`ps -ef | grep aaaaa | grep AAAAA | awk '{print $1}'`
                        if [[ -z $check1 ]]; then
                                return $OCF_ERROR
                        fi
                        if [[ ! -z  $check1 ]]; then
                                return $OCF_SUCCESS
                        fi
                else
                return $OCF_STOPPED
                fi
}

----

In which basically I check if the .pid file exists and if it does exist and the process (using the ps command) does not exists then I throw the OCF_ERROR but if it does exist the its the OCF_SUCCESS. If none exists then its the OCF_STOPPED.

My question here is (since Im not good with scripting).. would it be any better way to write this code? I mean make it more compact and intelligent?

Basically what I need is to check if the process is Online, Offline or Faulted and the states for these are:
Online = have pid file and ps shows the process
Offline = no pid file and no process
Faulted = have pid file and no process

And the responses should be OCF_SUCCESS, OCF_STOPPED and OCF_ERROR respectfully.

Tks,
Joao
joaotellesAsked:
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.

simon3270Commented:
It looks pretty efficient as it is.  The only excess code is the second test on $check1. If the first test succeeds, the second will fail, and vice versa.  Replace it with:
                        if [[ -z $check1 ]]; then
                                return $OCF_ERROR
                        else
                                return $OCF_SUCCESS
                        fi

Open in new window

0
simon3270Commented:
Checking again, there are a couple of other things to look at.  You have a file with the PID in it. so you could check that the process you find in the "if" test is the right one.  Alternatively, you could just check whether the process in the PID file is running.

Also, if the pid file doesn't exist, you need to check whether the process is actually running - I would return that as an error too.  So we have:
                file='/var/opt/dpa/pidfiles/aaaaa_AAAAA.pid'
                if [ -f $file ]; then
                        if kill -0 $(cat $file) >/dev/null; then
                                return $OCF_SUCCESS
                        else
                                return $OCF_ERROR
                        fi
                else
                        check1=`ps -ef | grep aaaaa | grep AAAAA | awk '{print $1}'`
                        if [[ -z  $check1 ]]; then
                                return $OCF_ERROR
                        else
                                return $OCF_STOPPED
                        fi
                fi

Open in new window

0

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
joaotellesAuthor Commented:
Very nice, havent considered the kill -0...nice one..... Tks!
0
Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

jmcgOwnerCommented:
Yes, kill -0 is better than using ps for generality, but you do need to be running with privileges or with the correct user-id to match the process.

I should say more about deprecating use of ps: as you may see in your case, the pipeline contains grep commands which appear in the listing from ps but are not the processes that you are looking for, so must be eliminated from the list by suitably clever uses of grep. There may be race conditions or the process may have execed a different program, so the ps method is less reliable than the pidfile method. [Of course, a bad daemon could fork into a different PID and thereby also hide from the reaper, but that's a misbehavior that should cause you to reject using the daemon in the first place.]
0
Dan CraciunIT ConsultantCommented:
AFAIK, "kill -0" will return 1 in 2 cases:
- if the process with that ID is not running (No such process)
- if the process is running but you don't have the right to send signals to it (Operation not permitted).

If there is a possibility that the script will run as another restricted user, you may want to check the output of kill -0 to decide if the process is running.

HTH,
Dan
0
joaotellesAuthor Commented:
Tks Dan, but is not the case, so it is ok to use kill -0.. but tks for the observation.
0
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.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.