4

I have a java app on my (Ubuntu) server. If I do this, then it starts correctly:

/usr/bin/java -cp /home/jenkins/veta/lily.jar com.sugarapp.lily.Main 

but I don't know how to get its PID. I don't know how to stop it with an init.d script.

I have a different app, written in Clojure, and for it I was able to write an init.d script that works great. So I tried to refashion that init.d script for my Java app, and this is what I got:

WORK_DIR="/home/jenkins/veta"
NAME="lily"
JAR="lily.jar"
USER="jenkins"
DAEMON="/usr/bin/java"
DAEMON_ARGS=" -cp /home/jenkins/veta/lily.jar com.sugarapp.lily.Main"

start () {
  echo "Starting lily..."
  if [ ! -f $WORK_DIR/lily.pid ]; then
    /sbin/start-stop-daemon --start --verbose --background --chdir $WORK_DIR --exec $DAEMON --pidfile $WORK_DIR/lily.pid --chuid "$USER" --make-pidfile -- $DAEMON_ARGS
  else
    echo "lily is already running..."
  fi
}

stop () {
    echo "Stopping lily..."
    /sbin/start-stop-daemon --stop --exec $DAEMON --pidfile $WORK_DIR/lily.pid
    rm $WORK_DIR/lily.pid
}

But this doesn't work. Although the PID in $WORK_DIR/lily.pid changes every time I run the script, no process with that PID ever seems to run. If I try:

ps aux | grep java

I don't see this app, nor if I try using the PID.

So is there a way I can use the first command, but somehow capture the PID, so I can store it for later?

I just want a reliable way to stop and start this app. That can be by PID or some other factor. I'm open to suggestions.

UPDATE:

Maybe my question is unclear? Something like jps or ps will give me too many answers. If I do something like "ps aux | grep java" I'll see that there are 5 different java apps running on the server. The start-stop-daemon won't know which PID belongs to this particular app, nor can I figure out what I should feed into my init.d script.

charlottesville
  • 467
  • 5
  • 19
  • possible duplicate of [How can a Java program get its own process ID?](http://stackoverflow.com/questions/35842/how-can-a-java-program-get-its-own-process-id) – Havenard Aug 03 '15 at 15:39

2 Answers2

3

If your system has jdk installed there is an utility called jps which resides in jdk/bin. It will display the list of running java process. Make use of it.

If jdk is not installed in your machine then you have to grep the java process from ps -eaf command.

Shriram
  • 4,343
  • 8
  • 37
  • 64
  • To add to the answer, here is the command to use if jps is not available: ps -eaf | grep java – hooknc Aug 03 '15 at 15:43
  • jps and ps give me too many answers. If there are 7 Java apps running on the server, jps will list all 7. I need to be able to find out what PID is being generated by this command: /usr/bin/java -cp /home/jenkins/veta/lily.jar com.sugarapp.lily.Main Only then can I make use of it in the init.d script. – charlottesville Aug 03 '15 at 20:56
2

If you want the pid from the command line, this might work:

myCommand & echo $!

Which I copied from the accepted response to a very similar topic in ServerFault: https://serverfault.com/a/205504

Community
  • 1
  • 1
Javier
  • 678
  • 5
  • 17