Bug 424982 - "jetty.sh status" Naively checks for existence of PID file
Summary: "jetty.sh status" Naively checks for existence of PID file
Status: RESOLVED FIXED
Alias: None
Product: Jetty
Classification: RT
Component: server (show other bugs)
Version: 9.1.0   Edit
Hardware: PC Unix All
: P3 major (vote)
Target Milestone: 9.1.x   Edit
Assignee: Greg Wilkins CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2014-01-07 04:05 EST by Croesus Kall CLA
Modified: 2014-04-25 06:57 EDT (History)
2 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Croesus Kall CLA 2014-01-07 04:05:00 EST
The jetty.sh script included with jetty 9.1.0 uses the following statement to see if jetty is running:

if [ -f "$JETTY_PID" ]
    then
      echo "Jetty running pid=$(< "$JETTY_PID")"
      exit 0
    fi
    exit 1
fi

This will always give a positive result if the PID file exists, even if the jetty server is not running. If jetty.sh is setup as an init.d service then the OS will not suspect that jetty is not running.

A more robust alternative would be to open the PID file and check if the pid is running, i.e.

if [ -f "$JETTY_PID" ]
    then
     local PID=$(< "$JETTY_PID")
     local PID_EXISTS=$(ps -p $PID 2>&1 )
     local PID_EXISTS_STATUS=$?
     if [[ $PID_EXISTS_STATUS -eq 0 ]]; then 
      echo "Jetty running pid=$PID"
      exit 0
     fi
     exit 1
fi


(One other thing, the jetty script does contain a utility function "running" which uses a "kill -0 <pid>" check in some cases. This is not as flexible as using "ps -p" since it will fail if you are not root or the jetty user)
Comment 1 Croesus Kall CLA 2014-01-07 09:42:23 EST
Small error in my proposed fix, here is an improved version:

if [ -f "$JETTY_PID" ]
    then
     local PID=$(< "$JETTY_PID")
     local PID_EXISTS
     PID_EXISTS=$(ps -p $PID 2>&1 )
     if [[ "$?" -eq 0 ]]; then 
      echo "Jetty running pid=$PID"
      exit 0
     fi
     exit 1
fi
Comment 2 Greg Wilkins CLA 2014-03-20 22:49:01 EDT
Already fixed in 9.1.x

but thanks anyway.
Comment 3 Croesus Kall CLA 2014-04-23 04:15:28 EDT
Hi,

Where has this been fixed ?

The newest changeset in github still has this issue:

https://github.com/eclipse/jetty.project/blob/master/jetty-distribution/src/main/resources/bin/jetty.sh#L553
Comment 5 Greg Wilkins CLA 2014-04-25 06:57:59 EDT
with commit ce5af1d  I have improved the running function and used it throughout the shell script:

running()
{
  if [ -f "$1" ]
  then
    local PID=$(cat "$1" 2>/dev/null) || return 1
    kill -0 "$PID" 2>/dev/null
    return
  fi
  rm -f "$1"
  return 1
}

thanks