0

I currently have 2 bash scripts:

1) tomcat.sh

#!/bin/bash
case "$1" in
    'start')
        /home/testuser/start.sh
        ;;
    'status')
        /home/testuser/status.sh
        ;;
 esac

2) status.sh

#!/bin/bash
COUNT="$( ps -ef | grep tomcat| wc -l )"
echo ${COUNT}
if [ "${COUNT}" -eq 2 ]
then
    echo "Tomcat is running."
else
    echo "Tomcatis not running"
fi

When I check status via these two methods:

./tomcat.sh status: ${COUNT} echos a value of 4.

status.sh: ${COUNT} echos value of 2.

I'm not sure why there is a discrepancy. I'm expecting both values from echo to match since they are essentially executing status.sh. Am I missing something?

EDIT: Added in the actual search values I'm using.

7
  • 2
    If you use grep to filter ps output, there will be one more process matching - the grep itself. This is the case for "grep appname", which will find a process named appname, and will also find another process "grep appname" :-) -- you could try pidof(1) Commented Jul 19, 2018 at 5:03
  • or: man pgrep Commented Jul 19, 2018 at 5:09
  • Thanks @linuxfan and @Cyrus. Although I'm still confused as to why running it via ./app.sh status and ./status.sh yields a different echo value. Shouldn't both be the same since it's essentially running the script status.sh? Commented Jul 19, 2018 at 5:28
  • 1
    @TimothyT. Maybe; that ps | grep pipeline is timing-dependent, so it can vary unpredictably based on apparently irrelevant things. But that should only add one to the match count, not two. Does the name of the outer script ("app.sh") match the app name as well? Commented Jul 19, 2018 at 5:51
  • 3
    Try either using pgrep (which exists specifically to avoid this problem) or using a pattern for the app name that doesn't match itself (the standard trick is to put square brackets around one character in the name, e.g. grep "[a]ppname"). Either of those should give you saner results. If not, you'll have to try capturing the output rather than just counting lines, and see what processes are being found. Commented Jul 19, 2018 at 6:11

1 Answer 1

1

Your tomcat.sh is still running when the ps -ef in status.sh is running. So, in case of using tomcat.sh ps finds at least these:

  • tomcat.sh
  • the tomcat you are looking for
  • grep tomcat (if you pipe, processes are started right to left, so when ps is running, your grep is also already running)

Right now I am not sure where the 4th is coming from.

In case of just running the status script, it is not finding tomcat.sh and thus you have less results. A solution could be to make your grep more specific for your use case, or use something that is more specific for your task, like pgrep (although pgrep java will possibly also give you other unwanted processes).

Possible solution:

COUNT="$( ps -ef | grep "tomcat" | grep "org.apache.catalina.startup.Bootstrap" | wc -l )"

Edit: Using a pidfile is of course also a way of doing it. In the question you show something that looks like a startup script. So writing a pidfile when starting and then reading and using that pidfile when querying, you can know if the service is started. It won't work though if someone uses another way to start the service.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. I'm using a slightly different formula which works as well. I appreciate the explanation as to why the ps -ef command is returning more if i use tomcat.sh

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.