Solved

Remove grep usage ouput

Posted on 2010-11-09
6
356 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 15

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
On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

 
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
Utilizing an array to gracefully append to a list of EmailAddresses
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…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

739 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