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
.

- 197,895
- 55
- 485
- 740

- 353
-
A more complete answer is here: https://unix.stackexchange.com/questions/1924/create-services-in-linux-start-up-in-linux?newreg=4c8689303cb94ff58057fc88769c0880 Basically, in Ubuntu you can create a script for /etc/init.d which can start/stop/restart your service. – Mr Ed Apr 16 '14 at 11:35
2 Answers
Make sure you're on 14.04. Ubuntu 16.04 (and above) uses systemd, not Upstart.
A Upstart script is a script file placed at /etc/init/
and ending in .conf
.
It requires 2 sections: one to indicate when to start, and another with the command to execute.
The easiest script to start with your sample is:
# myprogram.conf
start on filesystem
exec /usr/bin/java -jar /path_to/program
Created as root under /etc/init/myprogram.conf
.
If your script requires more than one command line, use the script
section instead of the exec
line:
# myprogram.conf
start on filesystem
script
/usr/bin/java -jar /path_to/program
echo "Another command"
end script
To enable bash completion for your service, add a symlink into /etc/init.d
folder:
sudo ln -s /etc/init/myprogram.conf /etc/init.d/myprogram
Then try start and stop it:
sudo service myprogram start
According the upstart cookbook, you can create pre-start
/post-start
and pre-stop
/post-stop
commands to be executed.
Additionally, I read you want to check if a process is running. Check this question and maybe use the pre-start
section.

- 6,834
-
1Maybe you need make it executable with
sudo chmod +x /etc/init.d/myprogram
. CAUTION: your answered method not works in Ubuntu 16.04! – juliocesar Jan 30 '17 at 09:02 -
2
-
start on
is not a necessary stanza. As you can read here: Such a job can only be controlled by an Administrator. – SuB Dec 30 '17 at 15:51 -
@SuB yeap, but then the job will not start automatically, and it was clear that the question author want something acting like a service, i.e., starting automatically. – Rael Gugelmin Cunha Dec 30 '17 at 20:21
-
-
On Ubuntu16.04, I try to add kafka service, but said: Failed to start kafka.service: Unit kafka.service not found. – linrongbin Jan 15 '18 at 04:03
-
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 ;-)

- 384
- 2
- 4
-
1It'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 '14 at 11:52