Solved

how to check the status of my java application

Posted on 2016-07-14
8
142 Views
Last Modified: 2016-07-27
I have the following service wrapper for a java application. My coworker wants me to expand it to include "service XXX status" and echo back the current status. How could I add status check on this script?

#!/bin/sh
SERVICE_NAME=pms
PATH_TO_JAR=/data/servicename.jar
PID_PATH_NAME=/tmp/servicename-pid
cd /data
case $1 in
    start)
        echo "Starting $SERVICE_NAME ..."
        if [ ! -f $PID_PATH_NAME ]; then
            nohup java -jar $PATH_TO_JAR /tmp 2>> /dev/null >> /dev/null &
                        echo $! > $PID_PATH_NAME
            echo "$SERVICE_NAME started ..."
        else
            echo "$SERVICE_NAME is already running ..."
        fi
    ;;
    stop)
        if [ -f $PID_PATH_NAME ]; then
            PID=$(cat $PID_PATH_NAME);
            echo "$SERVICE_NAME stoping ..."
            kill $PID;
            echo "$SERVICE_NAME stopped ..."
            rm $PID_PATH_NAME
        else
            echo "$SERVICE_NAME is not running ..."
        fi
    ;;
    restart)
        if [ -f $PID_PATH_NAME ]; then
            PID=$(cat $PID_PATH_NAME);
            echo "$SERVICE_NAME stopping ...";
            kill $PID;
            echo "$SERVICE_NAME stopped ...";
            rm $PID_PATH_NAME
            echo "$SERVICE_NAME starting ..."
            nohup java -jar $PATH_TO_JAR /tmp 2>> /dev/null >> /dev/null &
                        echo $! > $PID_PATH_NAME
            echo "$SERVICE_NAME started ..."
        else
            echo "$SERVICE_NAME is not running ..."
        fi
    ;;
esac
0
Comment
Question by:Jason Yu
[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
  • 4
  • 4
8 Comments
 
LVL 79

Accepted Solution

by:
arnold earned 500 total points
ID: 41711656
Add a check on whether the current PID that you wrote out, $PID_PATH_NAME, is running and is correct, I.e. Running the Java app.
Another way is to use ps -ef | grep Java |grep -v and check whether the response matches the PID you stored when the spa started.
Does the Java app provide for a way to check whether it is listening/responding?
0
 

Author Comment

by:Jason Yu
ID: 41728264
Hi, Arnold, thanks for your reply, I was busy with another project last week and didn't update on time.

Now I have to work on this again.

I modified the script by importing a system function @/etc/init.d/functions, and added status check through calling the "status function" defined in this system function(/etc/init.d/functions). However, it couldn't check the status correctly, it always shows "pms dead but subsys locked" (pms is the service/java application name). Do I need change my script to check the pid file as you suggested?

I am following this article to do the java application status check : "http://www.cyberciti.biz/tips/linux-write-sys-v-init-script-to-start-stop-service.html".


#!/bin/sh
#
# chkconfig: 35 90 12
# description: Foo server
#
# Get function from functions library
. /etc/init.d/functions
# Start the service pms
SERVICE_NAME=pms
PATH_TO_JAR=/data/AveryRollProduction.jar
JVM_ARGS="-Dspring.profiles.active=dev"
PID_PATH_NAME=/tmp/pmsService-pid
cd /data
case $1 in
    start)
        echo "Starting $SERVICE_NAME ..."
        if [ ! -f $PID_PATH_NAME ]; then
            nohup java $JVM_ARGS -jar $PATH_TO_JAR /tmp 2>> /dev/null >> /dev/null &
                        echo $! > $PID_PATH_NAME
### Create the lock file ###
        touch /var/lock/subsys/pms
            echo "$SERVICE_NAME started ..."
        else
            echo "$SERVICE_NAME is already running ..."
        fi
    ;;
    stop)
        if [ -f $PID_PATH_NAME ]; then
            PID=$(cat $PID_PATH_NAME);
            echo "$SERVICE_NAME stoping ..."
            kill $PID;
### Now, delete the lock file ###
        rm -f /var/lock/subsys/pms
            echo "$SERVICE_NAME stopped ..."
            rm $PID_PATH_NAME
        else
            echo "$SERVICE_NAME is not running ..."
        fi
    ;;
    status)
            status pms
    ;;

    restart)
        if [ -f $PID_PATH_NAME ]; then
            PID=$(cat $PID_PATH_NAME);
            echo "$SERVICE_NAME stopping ...";
            kill $PID;
            echo "$SERVICE_NAME stopped ...";
            rm $PID_PATH_NAME
            echo "$SERVICE_NAME starting ..."
            nohup java $JVM_ARGS -jar $PATH_TO_JAR /tmp 2>> /dev/null >> /dev/null &
                        echo $! > $PID_PATH_NAME
            echo "$SERVICE_NAME started ..."
        else
            echo "$SERVICE_NAME is not running ..."
        fi
    ;;
esac

Open in new window

0
 

Author Comment

by:Jason Yu
ID: 41728267
Basically, I followed that article by importing the system function @/etc/init.d/functions

And then created a lock file when I start the application, deleted it when I stop. When I need check the status of the application, the script calls the "status" function defined inside the system function.

The result is when I check the status, it always shows

[root@pmsdev init.d]# service pms status
pms dead but subsys locked
0
10 Questions to Ask when Buying Backup Software

Choosing the right backup solution for your organization can be a daunting task. To make the selection process easier, ask solution providers these 10 key questions.

 
LVL 79

Expert Comment

by:arnold
ID: 41728777
The command status does not work.
The Java app is not name Pms so the status function will not find it in the process list, but since Pms is used for the lock filename. Which is matched and this is why you get the response you did.
I.e, ps -ef | grep pms finds no matches
Ls -l /var/lock/subsystem/pms matches, which is why you get the lock is in place message...
0
 

Author Comment

by:Jason Yu
ID: 41731626
I got it now. I used the following script to check the pid file to decide if the service is running. if the pid file exist, then it's running, if now, the service is not running, thank you very much.

status)
         if [ -e $PID_PATH_NAME ]; then
            echo "$SERVICE_NAME pid file exist, the $SERVICE_NAME service is running"

        else
            echo "$SERVICE_NAME pid file doesn't exist, the $SERVICE_NAME service is not running ..."
        fi
    ;;
0
 
LVL 79

Expert Comment

by:arnold
ID: 41731684
You should add another check to make sure the PID from the PID file is actually in the process table and is the process/application you expect it to be..
0
 

Author Comment

by:Jason Yu
ID: 41731800
how could I do this, can I compare the PID number saved in this file with the result of "ps -ef | grep java" command. But "ps -ef | grep java"  always gives me two lines of results.

[root@pmsdev init.d]# ps -ef | grep java
root     31395     1  3 10:32 pts/1    00:02:00 java -Dspring.profiles.active=dev -jar /data/AveryRollProduction.jar /tmp
root     31727 30119  0 11:39 pts/0    00:00:00 grep --color=auto java
[root@pmsdev init.d]# service pms status
pms pid file exist, the pms service is running
The pid number is 31395
[root@pmsdev init.d]#

Open in new window

0
 
LVL 79

Expert Comment

by:arnold
ID: 41732241
Yes,
You can get the PID
Then ps -ef | grep $pid| grep Java |grep -v grep

If you get a result from the above ...
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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

Introduction We as admins face situation where we need to redirect websites to another. This may be required as a part of an upgrade keeping the old URL but website should be served from new URL. This document would brief you on different ways ca…
Join Greg Farro and Ethan Banks from Packet Pushers (http://packetpushers.net/podcast/podcasts/pq-show-93-smart-network-monitoring-paessler-sponsored/) and Greg Ross from Paessler (https://www.paessler.com/prtg) for a discussion about smart network …
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
Suggested Courses

617 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