1

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?

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
  • I'd be interested in seeing a ps indicating the PID as well as the PPID. – mdpc Jan 16 '15 at 2:43
  • @mdpc In my ps PID is already there right or I missed anything? – arsenal Jan 16 '15 at 2:49
  • ok...what about the PPID? What I am looking for is the fact that perhaps you have multiple instances because the parent is spinning off forks of itself which sometimes manifests itself like this. – mdpc Jan 16 '15 at 2:50
  • How to find out about PPID? Is there any command you want me to run? – arsenal Jan 16 '15 at 2:52
  • Use appropriate options to the PS command. See the man page for more on your particular distribution. – mdpc Jan 16 '15 at 2:53
3

Get rid of the &. That makes it fork into the background, Upstart thinks the process died, then spawns a new one. Just have the exec line without the ampersand.

  • aahh.. Let me try this out. But doesn't upstart internally checks the pid of the process and make sure if it is not running then start it back again? – arsenal Jan 16 '15 at 3:08
  • Right but it thinks the pid is, say 4000, but it is really 4001 because the process was forked off. – CameronNemo Jan 16 '15 at 3:09
  • Still trying to understand this, sorry if I missed any basic stuff : if it thinks that pid is 4000 which will still be running right? then why it starts 4001 then? – arsenal Jan 16 '15 at 21:23
  • It thinks the PID is n, but the pid of the actual running app is something different (usually n+1 or n+2). So then it starts yet another instance, which will have a PID greater than the first started app. – CameronNemo Jan 16 '15 at 23:03
1

crontab -e, then add following item

* * * * * a=`ps aux | grep abc.jar | grep -v grep | wc -l`; test $a -eq 0 && java -j /path/to/abc.jar
  • It seems like you could simplify this: ps aux | grep -q "[a]bc.jar" || java -j /path/to/abc.jar. The command grep "[a]bc.jar" matches abc.jar, but not itself, so you don't need to grep -v grep. grep -q is quiet; it doesn't write any output (so it's like > /dev/null). You don't need to look at the output from grep; all you need to do is look at its exit status. – G-Man Mar 17 '15 at 16:15
0

See also daemontools (not the virtual CD mount software): https://cr.yp.to/daemontools.html

  • Could you expand on this mainly-a-link-Answer with how the OP would use daemontools to solve their problem? – Jeff Schaller Jan 21 '18 at 12:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.