Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 364
  • Last Modified:

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

0
saharey
Asked:
saharey
  • 3
  • 2
1 Solution
 
DonConsolioCommented:
> RESULT=`${PS} | grep $SEARCH | grep -v grep 2>/dev/null`

RESULT=`${PS} | grep $SEARCH 2>/dev/null | grep -v grep 2>/dev/null`
0
 
TRW-ConsultingCommented:
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
 
sahareyAuthor Commented:
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
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
TRW-ConsultingCommented:
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
 
sahareyAuthor Commented:
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
 
TRW-ConsultingCommented:
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: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now