Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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.

share|improve this question
add comment

1 Answer

up vote 7 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.

share|improve this answer
    
Is it necessary to restart for the changes to take effect? update-rc.d didn't work/ –  theTuxRacer 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
    
Unfortunately, your solution doesn't seem to work (anymore) for Jetty 8 as start.jar spawns another processes, which won't get killed with your approach. I created another question to address this problem: stackoverflow.com/questions/10796242/… –  Sebi May 29 '12 at 9:18
3  
@Sebi You might just need to stick an expect fork in there (before the script starts). –  Oli May 29 '12 at 9:38
show 1 more comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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