Skip to content

Instantly share code, notes, and snippets.

@yblee85
Last active August 29, 2015 14:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yblee85/fe57ed6e32c8d102d365 to your computer and use it in GitHub Desktop.
Save yblee85/fe57ed6e32c8d102d365 to your computer and use it in GitHub Desktop.
creating service in ubuntu 12.04
// origin : askubuntu.com (http://askubuntu.com/questions/351879/how-to-create-a-service-on-ubuntu-lts-12-04)
// about upstart manual : http://upstart.ubuntu.com/cookbook
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 command line, use the script section:
# myprogram.conf
start on filesystem
script
/usr/bin/java -jar /path_to/program
echo "Another command"
end script
To enable bash complete 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
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.
@yblee85
Copy link
Author

yblee85 commented Mar 4, 2015

example for java

create sh file in the same as jar file

test.sh
cd /dir/to/file/
java -jar utilApp.jar

@yblee85
Copy link
Author

yblee85 commented Jun 10, 2015

if you want to restart automatically when it crashes

/etc/init/yourservice.conf

Restart the process if it dies with a signal

or exit code not given by the 'normal exit' stanza.

respawn

Give up if restart occurs 10 times in 90 seconds.

respawn limit 10 90

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment