Linux service from runnable jar

777 views
Skip to first unread message

Sushrut Bidwai

unread,
Mar 5, 2013, 2:21:56 AM3/5/13
to java...@googlegroups.com
Hi,

What is the simplest way to create a service from runnable jar? My environment is Ubuntu 12.x 64 bit. Requirement is I should be able to start and stop the service. Something like /etc/init.d/myservice1 start, /etc/init.d/myservice1 stop and if possible restart.

Thanks,
SB

Ashish Belagali (Gmail)

unread,
Mar 5, 2013, 3:22:59 AM3/5/13
to java...@googlegroups.com
Dear Sushrut,

Each one of these services is basically a small shell script which responds to start / stop requests. (Open the files inside /etc/init.d with a text editor.) You will accordingly need to write one that does something like this:

Upon start:
Call java -jar "yourjar.jar"

Upon stop:
Use ps and grep to get the pid (process id)
Use kill to kill this process

Some applications store the pid in a file when the process starts. If you do that then you can bypass the ps/grep step and directly get the pid to kill from this file.

Make this script executable and now you will be able to call 'service start / stop' on it.

Hope this helps,
/Ashish
--
  Build your communication asset - kommbox.com
  Recruit efficiently - headtracker.in
  Outsource software development - acism.com


--
You received this message because you are subscribed to the Google Groups "Java User Group For Pune, India" group.
To unsubscribe from this group and stop receiving emails from it, send an email to javapune+u...@googlegroups.com.
To post to this group, send email to java...@googlegroups.com.
Visit this group at http://groups.google.com/group/javapune?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Sushrut Bidwai

unread,
Mar 5, 2013, 3:44:01 AM3/5/13
to java...@googlegroups.com
Just checking to see if there is something which I can use off the shelf. Found this - http://wrapper.tanukisoftware.com/doc/english/integrate-jar-nix.html , and trying it out. But seems a bit more complex and was hoping for a more simpler solution. 

You received this message because you are subscribed to a topic in the Google Groups "Java User Group For Pune, India" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/javapune/dsNqDqhQmOM/unsubscribe?hl=en.
To unsubscribe from this group and all its topics, send an email to javapune+u...@googlegroups.com.

To post to this group, send email to java...@googlegroups.com.
Visit this group at http://groups.google.com/group/javapune?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
 
 



--
Best Regards,
Sushrut

Shirish Padalkar

unread,
Mar 5, 2013, 4:21:41 AM3/5/13
to java...@googlegroups.com
I have put a small (quick and dirty) gist for init.d script. Check if it's useful - https://gist.github.com/shirish4you/5089019

With regards,
Shirish Padalkar
http://twitter.com/_Garbage_

गिरीश मांधळे

unread,
Mar 5, 2013, 3:51:55 AM3/5/13
to java...@googlegroups.com
Try this... Upstart for Ubuntu

Chinmay

unread,
Mar 5, 2013, 3:17:01 AM3/5/13
to JAVA PUNE

1.Put the java command in file and make it executable.

2.copy that script file into init.d dir

3.execute update-rc.d for that script (search on net for syntax)

Thats all!!

--

dexterous

unread,
Jun 12, 2013, 1:07:45 AM6/12/13
to java...@googlegroups.com

On Tuesday, March 5, 2013 2:21:55 PM UTC+5:30, Gireesh wrote:
Try this... Upstart for Ubuntu

Agreed, Upstart is the new shiznit for Ubuntu. It'll take you less than a day to catch on and it's a whole lot easier (and more feature-ful) than SysV Init scripts.

dexterous

unread,
Jun 12, 2013, 1:13:52 AM6/12/13
to java...@googlegroups.com

On Tuesday, March 5, 2013 1:47:01 PM UTC+5:30, Chinmay Walimbe wrote:

1.Put the java command in file and make it executable.

2.copy that script file into init.d dir

3.execute update-rc.d for that script (search on net for syntax)

dude! update-rc.d is a poor substitute for chkconfig (borrowed from RHEL tree and not available past Ubuntu 12.04) and an absolute mess in terms of command line options and arguments. I'd avoid this like a rash!

The sysv-rc-conf command appears to be better at this, but I haven't used it myself.

All said and done, I'd suggest you go for Upstart. It's really easy once you get it, which doesn't take too long.

- d

dexterous

unread,
Jun 12, 2013, 1:18:11 AM6/12/13
to java...@googlegroups.com

On Wednesday, June 12, 2013 10:43:52 AM UTC+5:30, dexterous wrote:

On Tuesday, March 5, 2013 1:47:01 PM UTC+5:30, Chinmay Walimbe wrote:

3.execute update-rc.d for that script (search on net for syntax)

dude! update-rc.d is a poor substitute for chkconfig (borrowed from RHEL tree and not available past Ubuntu 12.04) and an absolute mess in terms of command line options and arguments. I'd avoid this like a rash!

Refer here for my detailed views on the above => http://askubuntu.com/a/257759

:P

- d 

Sushrut Bidwai

unread,
Jun 12, 2013, 1:21:26 AM6/12/13
to java...@googlegroups.com
Thanks. I will try out upstart.


- d 

--
You received this message because you are subscribed to a topic in the Google Groups "Java User Group For Pune, India" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/javapune/dsNqDqhQmOM/unsubscribe?hl=en.
To unsubscribe from this group and all its topics, send an email to javapune+u...@googlegroups.com.

To post to this group, send email to java...@googlegroups.com.
Visit this group at http://groups.google.com/group/javapune?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
 
 



--
Best Regards,
Sushrut

Ranjib Dey

unread,
Jun 12, 2013, 1:31:38 AM6/12/13
to java...@googlegroups.com
 btw. you can take a look at jenkins and tomcat packages for  reference. Also, note runit is arguably a better replacement of upstart (though its not de-facto standard). In general, the standard practice is
1) u'll be dropping a config file which holds the JVM and other opts (like JAVA_HOME, JVM_OPTS , -XXfoo etc) in /etc/default (and this is going to be a standard shell script without any extension), 
2) an executable shell script to that eval/source the config file and pass the to java (- result of sourced config from 1) -jar foo.jar & (note the ampersand in the end forces it to go background)
3) grab the pid from  $! (bash  variable for last background process pid) and write it in /var/run/foo.pid ,
4) 1-3 will be used by the start switch case in the init script, 3 will be used by the status switch case in the init script and 3 will also be used by stop switch case (grab the pid and kill the process)
5) in ubuntu start-stop-daemon command facilitate a lot in these steps.

I strongly suggest u start with vanilla sys-v init script, then used start-stop-daemon and finally move to upstart / runit like. Those system do additional process supervision for you (i.e they'll restart the process in case it crashes), but since java bootup takes time, and depending upon the servlet container the master process pid might differ u can find strange behavior if you are not certain how its done. 


best 
ranjib 


--
You received this message because you are subscribed to the Google Groups "Java User Group For Pune, India" group.
To unsubscribe from this group and stop receiving emails from it, send an email to javapune+u...@googlegroups.com.

dexterous

unread,
Jun 12, 2013, 1:46:02 AM6/12/13
to java...@googlegroups.com


On Wednesday, June 12, 2013 11:01:38 AM UTC+5:30, rdeys wrote:
[snip] 
I strongly suggest u start with vanilla sys-v init script, then used start-stop-daemon and finally move to upstart / runit like. Those system do additional process supervision for you (i.e they'll restart the process in case it crashes), but since java bootup takes time, and depending upon the servlet container the master process pid might differ u can find strange behavior if you are not certain how its done.

If you want to learn how to daemonize processes nicely in *nix, go with the above approach. If you just want to get it done, use upstart!

<ducks from="ranjib" />

Seriously, Upstart does 2-5 for you for free. As for start-stop-daemon, just the sheer number of knobs and switches that thing has in relation to the functionality it offers just puts me off .it every time Trying to remember all those switches for a single command makes my brain explode!

- d

Ranjib Dey

unread,
Jun 12, 2013, 1:52:20 AM6/12/13
to java...@googlegroups.com

dexterous

unread,
Jun 12, 2013, 1:59:10 AM6/12/13
to java...@googlegroups.com

On Wednesday, June 12, 2013 11:22:20 AM UTC+5:30, rdeys wrote:

Excellent example. I knew we were going to get sysad gold once you jumped onto the list! :D

- d

PS- that example is missing the emits clause.

dexterous

unread,
Jun 12, 2013, 2:01:17 AM6/12/13
to java...@googlegroups.com
Also, you don't need to redirect logs; upstart manages them for you under /var/log/upstart.

- d 

Chinmay

unread,
Jun 12, 2013, 12:58:25 PM6/12/13
to JAVA PUNE
Now I realised that, was giving 2007 ka solution! :) Cool. Let me give a try to Upstart

Cheers!



- d 

--

Saager Mhatre

unread,
Jun 12, 2013, 2:29:11 PM6/12/13
to java...@googlegroups.com
On Wed, Jun 12, 2013 at 10:28 PM, Chinmay <chinmay...@gmail.com> wrote:
Now I realised that, was giving 2007 ka solution! :) Cool. Let me give a try to Upstart

In that case, you'll find it interesting to note that Fedora moved past Upstart and is now using SystemD! :)
SystemD is supposed to be even more powerful and some say scripting services in SystemD is easier than Upstart configs.
That said, I haven't tried SystemD yet, so caveat emptor.

- d

--
You received this message because you are subscribed to a topic in the Google Groups "Java User Group For Pune, India" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/javapune/dsNqDqhQmOM/unsubscribe?hl=en.
To unsubscribe from this group and all its topics, send an email to javapune+u...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages