Avatar of saharey
saharey
 asked on

Remove grep usage ouput

Before starting my tomcat server I want to see that "server.pid" file is not present and also that the no process is running with PID mentioned in this server.pid,
Scenario is  when ‘server.pid’ file is not present then I get this grep usage output, this happens while executing “start” or “status” arg

./serv.sh status
+ status
++ isRunning
++ '[' '!' -z /home/psaharey/XLRISoftware/XLRI/cdp/bin/server.pid ']'
+++ cat /home/psaharey/XLRISoftware/XLRI/cdp/bin/server.pid
++ SEARCH=
+++ /bin/ps -ef
+++ grep
Usage: grep [OPTION]... PATTERN [FILE]...
Try `grep --help' for more information
.
+++ grep -v grep
++ RESULT=
++ '[' -n '' ']'
++ echo 0
+ RET=0
+ '[' 0 -eq 0 ']'
+ echo 'The server is stopped'
The server is stopped

I want to go away with this usage output, can anyone help me out in this reagard.
My “isRunning” function looks like this
 
isRunning ()
{
   #PID=`cat "$CATALINA_PID" 2>/dev/null`
   if [ ! -z "$CATALINA_PID" ]; then
      SEARCH=`cat "${SERVER_PID}" 2>/dev/null`
    else
      SEARCH=$CATALINA
   fi
   RESULT=`${PS} | grep $SEARCH | grep -v grep 2>/dev/null`
   if [ -n "${RESULT}" ]; then
      # if we are here, then server is running
      echo 1
   else
      # if we are here, then server is not running
      echo 0
   fi
}

Open in new window

Shell Scripting

Avatar of undefined
Last Comment
TRW-Consulting

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
DonConsolio

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
TRW-Consulting

Depending on the version of Linux/UNIX you are using, you may have some 'ps' options that will do a better job. So let us know that if what follows is not enough.

Other than that, there are 2 problems here. This line:

  if [ ! -z "$CATALINA_PID" ]

should be:

  if [ -s "$CATALINA_PID" ]

because you're testing for the existence of a file that has something in it, not the size of a character string.

The next problem is where you are grep'ing on a $SEARCH string, which could be a process id. There is a very good chance of finding something you're not looking for. For example, what if the search string was for process id 732?  And then, what if process ID 2732, 7320, or any other process with the number 732 in it was running.  The grep would succeed even though it's not really what you're looking for.
saharey

ASKER
Thanks TRW for pointing out the issue in my “isRunning” function, if I use

if [ -s "$CATALINA_PID" ]; then

in my “isRunning” function will it take care of both issues you mentioned means content of server.pid file as well grep $SEARCH string
TRW-Consulting

No, it won't take care of the grep issue.  That's why I was asking about the version of Linux/UNIX you are running, and the options the 'ps' command has.

If the options were available it would be better to use the 'ps -p $PID' option to find any running process with the process ID found.  And it would be better to use 'ps -C $CATALINA'  to find any running processes with a particular command name if the process ID wasn't available.  But that all depends on whether or not those options are available with the version you have.
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
saharey

ASKER
I cannot restrict user to use particular version of Sol or Linux, as we support Mac, Sol, Lin, SuSe Lin and Win so I have to satisfy all OS. So I cannot play much with ps options as last time when I tried a different  ps options for Sol one of our customer raised a high severity bug just because after upgrade he was unable to start the server.
TRW-Consulting

If you're trying to make one script operate in all those different environment then you're going to have to build a whole lot of smarts into it.  And I don't even know how you're thinking of doing this on Windows!

You would have to interrogate the environment to determine what commands to use in each environment.

Like I said before, the danger with just grep'ing for a process ID is that other processes have the same digits in them, but can still be an entirely different process (like 72, 372, 3720, 2372, etc). Also, processes have both a process ID and a parent process ID, doubling the risk.