Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I have a Java executable program that I can run by typing java -jar abc.jar in terminal. How can I run it as a service? I want to run it as a service like by typing service abc start.

share|improve this question
    
A more complete answer is here: unix.stackexchange.com/questions/1924/… Basically, in Ubuntu you can create a script for /etc/init.d which can start/stop/restart your service. –  Mr Ed Apr 16 at 11:35

3 Answers 3

up vote 1 down vote accepted

You need to create an upstart. http://upstart.ubuntu.com/getting-started.html

Upstart is (IMHO) a disaster compared to good ol' SysV init scripts. Upstart is FAR more effort with little upside to the added work. With that said, I suspect there will be a few upstart defenders out there that will take me to task for me stating the obvious ;-)

share|improve this answer
    
It's just a file in /etc/init/yourservice.conf with a line indicating when to start, and another to the exec command. Fedora uses upstart too. Additionally, even Debian is changing its starting schema to SystemD, which will be Ubuntu schema in the future. –  Rael Gugelmin Cunha Apr 16 at 11:52

I can give you some best alternative than that, that is using alias. Its rather easier than doing service abc start.

you can simply call it everytime when ever you want with abc. If you want to do it , then open your terminal and type as

gedit .bashrc

then at the end type a line as

alias abc=' ava -jar abc.jar '

Then save and close the file.

Then restart the bash with source ~/.bashrc from your terminal.

Now if you type abc in your terminal automatically it will access the jar file you have given.

share|improve this answer
2  
Many thanks, this information is very useful. Actually my point is that I want to check if my process is running or not running easily and I want it to run on background. If I create an alias like you said, can I see the process name as "abc" in process list after typing "ps -A"? –  Mecek Sep 30 '13 at 14:50
    
If its running...then you can. –  AgentCool Sep 30 '13 at 15:03

Upstart scripts have to placed at /etc/init, ending with .conf.

Basically they require 2 sessions: one to indicate when to start, and another with the command to exec.

The easiest script to start with your sample is:

# myprogram.conf
start on filesystem
exec /usr/bin/java -jar /path_to/program

Of course, created as root under /etc/init/myprogram.conf.

If your script requires more than one exec line, use the script section:

# myprogram.conf
start on filesystem
script
    /usr/bin/java -jar /path_to/program
end script

Then try start and stop it:

sudo service myprogram start

Of course, if you read the upstart cookbook, you can create pre-start/post-start and pre-stop/post-stop commands to be executed.

And, additionally, I read you want to check if a process is running. Check this question and maybe use the pre-start section.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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