Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

how to check the status of my java application

Posted on 2016-07-14
8
Medium Priority
?
159 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 2000 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
Stack Overflow Podcast - Developer Story

Welcome to the Stack Overflow podcast recorded Thursday July 20 at Stack Overflow Headquearters in NYC. Your hosts today are podcast regulars Jay Hanlon, David Fullerton, and Ilana Yitzhaki, plus the quite irregular Matt Sherman (Stack Overflow Engineering Manager extraordinaire)

 
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

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 …
I. Introduction There's an interesting discussion going on now in an Experts Exchange Group — Attachments with no extension (http://www.experts-exchange.com/discussions/210281/Attachments-with-no-extension.html). This reminded me of questions tha…
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.
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…
Suggested Courses

705 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