Tuesday, May 14, 2024
 Popular · Latest · Hot · Upcoming
42
rated 0 times [  42] [ 0]  / answers: 1 / hits: 69381  / 3 Years ago, wed, june 16, 2021, 8:51:46

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.


More From » java

 Answers
3

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.


[#29216] Thursday, June 17, 2021, 3 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
piscen

Total Points: 134
Total Questions: 117
Total Answers: 133

Location: Indonesia
Member since Wed, Jul 7, 2021
3 Years ago
;