Solved

Remove grep usage ouput

Posted on 2010-11-09
6
344 Views
Last Modified: 2012-06-27
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

0
Comment
Question by:saharey
  • 3
  • 2
6 Comments
 
LVL 14

Accepted Solution

by:
DonConsolio earned 50 total points
ID: 34099709
> RESULT=`${PS} | grep $SEARCH | grep -v grep 2>/dev/null`

RESULT=`${PS} | grep $SEARCH 2>/dev/null | grep -v grep 2>/dev/null`
0
 
LVL 10

Expert Comment

by:TRW-Consulting
ID: 34107189
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.
0
 

Author Comment

by:saharey
ID: 34120847
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
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 10

Expert Comment

by:TRW-Consulting
ID: 34122111
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.
0
 

Author Comment

by:saharey
ID: 34133994
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.
0
 
LVL 10

Expert Comment

by:TRW-Consulting
ID: 34142637
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.
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Background Still having to process all these year-end "csv" files received from all these sources (including Government entities), sometimes we have the need to examine the contents due to data error, etc... As a "Unix" shop, our only readily …
Recently, an awarded photographer, Selina De Maeyer (http://www.selinademaeyer.com/), completed a photo shoot of a beautiful event (http://www.sintjacobantwerpen.be/verslag-en-fotoreportage-van-de-sacramentsprocessie-door-antwerpen#thumbnails) in An…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now