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.

link|improve this question

feedback

1 Answer

up vote 5 down vote accepted

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.

link|improve this answer
Is it necessary to restart for the changes to take effect? update-rc.d didn't work/ – Kaustubh P Jan 18 '11 at 11:35
You don't need update-rc.d for upstart jobs. – Oli Jan 18 '11 at 11:57
upstart watches /etc/init using inotify, so it generally knows about all edits to the files underneath. No need to do anything except 'start my-jetty-jar'. Oh, and @Oli , you can definitely have a job with no start on. Note that stop on runlevel [!2345] may be flagged in the future as a bad way to stop things (since it doesn't guarantee that they'll stop before filesystems are unmounted) – SpamapS Jan 21 '11 at 23:31
feedback

Your Answer

 
or
required, but never shown

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