Skip to main content
added 606 characters in body
Source Link
arsenal
  • 3.1k
  • 17
  • 49
  • 50

UPDATE:-

This is what I got for PPID -

david@machineA:~$ ps aux | grep java
david  18454  4.5  0.0 33906208 57520 ?      Sl   20:01   0:01 /usr/lib/jvm/java-1.7.0-openjdk-amd64/bin/java -jar abc.jar config.properties
david  18692 27.3  0.0 33906208 57788 ?      Sl   20:01   0:01 /usr/lib/jvm/java-1.7.0-openjdk-amd64/bin/java -jar abc.jar config.properties
david    18779  0.0  0.0   8096   940 pts/2    S+   20:02   0:00 grep --color=auto java


david@machineA:~$ ps  xao pid,ppid,pgid,sid,comm | grep java
 18454      1  18453  18453 java
 18692      1  18691  18691 java

UPDATE:-

This is what I got for PPID -

david@machineA:~$ ps aux | grep java
david  18454  4.5  0.0 33906208 57520 ?      Sl   20:01   0:01 /usr/lib/jvm/java-1.7.0-openjdk-amd64/bin/java -jar abc.jar config.properties
david  18692 27.3  0.0 33906208 57788 ?      Sl   20:01   0:01 /usr/lib/jvm/java-1.7.0-openjdk-amd64/bin/java -jar abc.jar config.properties
david    18779  0.0  0.0   8096   940 pts/2    S+   20:02   0:00 grep --color=auto java


david@machineA:~$ ps  xao pid,ppid,pgid,sid,comm | grep java
 18454      1  18453  18453 java
 18692      1  18691  18691 java
Source Link
arsenal
  • 3.1k
  • 17
  • 49
  • 50

How to restart my java program automatically if it gets killed?

I am running my runnable jar in linux machine (machineA) as shown below. In the runnable Jar, I have a java program which sends email to me every 15 mniutes.

/usr/lib/jvm/java-1.7.0-openjdk-amd64/bin/java -jar abc.jar config.properties &

As soon as I start my abc.jar like above, it will be running in the background and there is a class which will keep on sending me email every 15 minutes. I am using Scheduler in my java program which is a thread which wakes up every 15 minutes and send me an email.

Now everything is working fine. Suppose machineA got restarted for some reason or abc.jar got killed for whatever reason, then I am looking for a way so that my abc.jar gets started in the background again automatically.

So I decided to use upstart feature in Ubunutu as I am running Ubuntu 12.04 - And here is my config file -

#/etc/init/testlnp.conf

#sudo start testlnp
#sudo stop testlnp

start on runlevel [2345]
stop on runlevel [016]

chdir /export/home/david/tester
respawn

post-stop script
  sleep 30
end script

limit nofile 8092 8092
setuid david
exec /usr/lib/jvm/java-1.7.0-openjdk-amd64/bin/java -jar abc.jar config.properties &

I have my abc.jar file in this directory /export/home/david/tester. Now I started my java program like this one time -

sudo start testlnp

And it started fine, I can see through ps aux | grep java -

david@machineA:~$ ps aux | grep java
david 130691 38.5  0.0 33906208 58636 ?      Sl   19:24   0:01 /usr/lib/jvm/java-1.7.0-openjdk-amd64/bin/java -jar abc.jar config.properties
david   131029  0.0  0.0   8100   936 pts/2    S+   19:24   0:00 grep --color=auto java

Now after some time, I did ps aux | grep java again and I saw like this - Meaning multiple instances of my abc.jar program? This is what I am not able to understand why it happened?

david@slc4b03c-8ixd:~$ ps aux | grep java
david   1746  4.5  0.0 33906208 57808 ?      Sl   19:25   0:01 /usr/lib/jvm/java-1.7.0-openjdk-amd64/bin/java -jar abc.jar config.properties
david   2143 73.0  0.0 33906208 57992 ?      Sl   19:25   0:01 /usr/lib/jvm/java-1.7.0-openjdk-amd64/bin/java -jar abc.jar config.properties
david     2180  0.0  0.0   8100   936 pts/2    S+   19:25   0:00 grep --color=auto java
david 130691  2.5  0.0 33906208 57492 ?      Sl   19:24   0:01 /usr/lib/jvm/java-1.7.0-openjdk-amd64/bin/java -jar abc.jar config.properties

My main aim is to restart abc.jar again if my machine gets restarted or abc.jar gets killed for whatever reason? How do I achieve this? Is there anything wrong I am doing with upstart?