Ubuntu – Want to make an upstart script; need help and advice

servicesupstart

Atm, I need to randomly start and stop (for lack of a better word in my mind) a job. I start it by typing java -jar foo.jar and to stop it find out its pid and kill it. Killing it doesn't cause any data loss or corruption or anything, just FYI. Its tedious to do both these steps because the first command has to be executed from a particular directory, ie /usr/share/jetty (The kill can be executed from anywhere).

So I was thinking something on the lines of

service foo start and service foo stop to start and stop services. Is that possible, and more importantly correct? Is there any other solution?

Thanks.

Best Answer

Yeah Upstart is a pretty good option for this. Just create a new file:

sudoedit /etc/init/my-jetty-jar.conf

You can change the filename to whatever you like but then stick this in it:

description     "Run my jetty jar"

# no start option as you might not want it to auto-start
# This might not be supported - you might need a: start on runlevel [3]
stop on runlevel [!2345]

# if you want it to automatically restart if it crashes, leave the next line in
respawn

script
    cd /usr/share/jetty
    su -c "/usb/bin/java -jar /path/to/foo.jar" nobody
end script

Some notes about that, I'm suing to nobody for security. You might need it to su into another account but it's probably not recommended that it runs as root.