0

I am writing a simple Linux shell script which checks the status of services running and if any process is not running it will display the name of the process which is not running.

I have tried below and stuck now in else statement. What I want to output an else statement is the name of variable out of the 4 displayed which is having a non zero value.

The output should be like below if RUNWRAPPERSTATUS and EKYCSTATUS are non zero.

There is some while starting RUNWRAPPER AND EKYC . Kindly verify

Actual Code starts here:

ps -aef | grep -i NGEjbClient
RUNWRAPPERSTATUS=$(echo $?)

ps -aef | grep -i startSMS
RUNSTATUS=(echo $?)

ps -aef | grep -i DirectoryService
DISSTATUS=(echo $?)

ps -aef | grep -i EKYCUploadWorkitem
EKYCSTATUS=(echo $?)


if [ $RUNWRAPPERSTATUS -eq 0 ] && [ $RUNSTATUS -eq 0 ] && [ $DISSTATUS -eq 0 ]  && [ $EKYCSTATUS -eq 0 ]

then
    echo "Jboss Services and App Services started"
1
  • var=(echo $?) is an array assignment, maybe you want $( ? All $RUNSTATUS $DISSTATUS $EKYCSTATUS will be equal to echo. Commented Sep 11, 2019 at 7:55

2 Answers 2

2

A simple script that showcases what you could do:

SERVICES="NGEjbClient startSMS DirectoryService EKYCUploadWorkitem"
for service in $SERVICES
do
    if ! pgrep $service
    then
        echo "$service is not running. Please verify"
    fi
done

NOTES:

  • use pgrep to search in process names
  • use a loop to avoid duplicate code if possible.
Sign up to request clarification or add additional context in comments.

8 Comments

pgrep is not working in my case as NGEjbClient is not the script name but argument to the script. ps -aef | grep NGEjbClient works and gives output
! pgrep $service -- what does this mean
pgrep : search a process whose name contains the string.
@rizwan, the ! is to negate the exit status: if ! COMMAND; then A; else B; fi executes A if COMMAND "failed", and B if it "succeeded".
@rizwan pgrep exists specifically as a cleaner replacement for the ps | grep someprocess | grep -v grep kluge (see my answer). If you have it available on your system, it's a better tool for this job.
|
1

Rather than collecting exit statuses in separate variables, I'd make an array of failed service names. I'd also just loop over the services, rather than writing an explicit separate test for each one.

But there's another problem: the ps | grep something pattern tends to find the grep command, which gives false positives. One way to avoid this is to add a grep -v grep to the pipeline.

If you don't want to print the found process entry, add the -q option to the last grep in the pipe. Important: it must be the last one or it won't work.

failedServices=()    # This creates an empty array

for service in NGEjbClient startSMS DirectoryService EKYCUploadWorkitem; do
    ps -aef | grep -i "$service" | grep -vq grep ||    # Remove "q" option to print matches
        failedServices+=("$service")    # +=() appends to the array
done

if (( ${#failedServices[@]} == 0 )); then    # This tests the number of elements in the array
    echo "Jboss Services and App Services started"
else
    echo "Some service(s) have not started:" "${failedServices[@]}"
fi

4 Comments

grep -q to avoid needless output ? otherwise nice solution.
@ChrisMaes Good idea; I've added it to my answer.
@GordonDavisson grep -q string always give exit status of 0 even if no string found
@rizwan That shouldn't be the case. Note that grep -qv string should exit with nonzero status only if no lines not matching "string" are in the input (i.e. if either intput's empty, or every line contains something matching "string"). This can be a bit confusing. It's possible you have a version of grep that doesn't handle the -qv combination properly; try swapping the order, i.e.: ps -aef | grep -v grep | grep -iq "$service"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.