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.

When I start up my development environment, there are a variety of processes I need to run in the background. It is a pain to start them all individually so I want to write a script that starts each one. I can do this just fine but the problem comes when I need to restart them (something I will need to do regularly).

What is the easiest way to capture the process upon starting it and saving that information so that when I run the script again, it will check to see if that information has been saved and then close down those processes before restarting them.

I also want the script to be flexible enough such that if I shutdown the process manually, it will a) not throw an error if it can't find the process and b) not accidentally shut down some other process that has since then shared whatever identifying information I have stored.

Update

More specifically, I want to at least do the following at the moment:

1) Type a short command like "start-dev"

2) Execute the following (Note that I want the second and third commands to run in the background and am using & to do so but not the final command as I want to see the output from passenger as it runs):

  1. Change to my working directory
  2. Start faye as a background process
  3. Start watchr as a background process
  4. Start passenger

So far I have got this

#!/bin/bash
cd ~/path/to/my/working-directory
rackup faye.ru -s thin -E production &
watch refresh.watchr &
passenger start

This works fine but the problem comes when I need to restart all these processes. I first have to track down all their process ids and then kill them before running start-dev again. Therefore I would like to:

4) Type a short command like "restart-dev" which tracks down the processes I have previously run in the background and then kill them before running "start-dev" again. It needs to be able to not throw an error if I have shut down any of these manually and not accidentally shutdown an incorrect process.

share|improve this question
1  
I'm thinking it should be sent to SuperUser.com, unless it's a Ubuntu-specific question, which the way it's written currently it's not. –  jrg Apr 5 '11 at 14:13
    
Heya James. Thanks for your reply. Is there any way to do this automatically (I have seen some questions moved in this way but only on the main 3 stack sites) or should I just repost my question there? –  Rupert Madden-Abbott Apr 5 '11 at 14:29
    
You're welcome Rupert. And I'll flag it for moving. :) –  jrg Apr 5 '11 at 14:34
    
@James I disagree. This is perfectly within our remit especially as the available methods for doing this sort of thing on various distributions can differ. –  Oli Apr 5 '11 at 14:53
1  
@James Permission granted :) –  Oli Apr 5 '11 at 15:08

3 Answers 3

up vote 4 down vote accepted

I'd tackle it something like this.

#!/bin/bash  

startme() {
    cd ~/path/to/my/working-directory
    rackup faye.ru -s thin -E production &
    watch refresh.watchr &
    passenger start
}

stopme() {
    pkill -f "rackup faye.ru" 
    pkill -f "watch refresh.watchr"
}

case "$1" in 
    start)   startme ;;
    stop)    stopme ;;
    restart) stopme; startme ;;
    *) echo "usage: $0 start|stop|restart" >&2
       exit 1
       ;;
esac
share|improve this answer
1  
This worked great but I had to change the last line from "done" to "esac". Took me a few seconds to work out what the hell an esac was :) –  Rupert Madden-Abbott Apr 5 '11 at 21:59
    
@Rupert, thanks, brainfart. –  glenn jackman Apr 5 '11 at 23:00
    
@Rupert me too, lol. And I used today! –  H_7 Mar 25 '12 at 1:11

I'd say you probably want to have a go at writing an upstart script to manage these processes. Upstart is a tool that is perfect for managing system daemons but also lends itself to any sort of process where you want to be able to control its status.

An upstart script will keep track of a process but it also lets you script things to happen before you stop it.

stop on runlevel [06]
expect fork
respawn

script
    # run your process here
    # you might want to su to change user from root
end script

pre-stop script
    # things in here will run before the command is halted
end script

Save that as /etc/init/devenvironment.conf and then you should be able to control it with:

sudo start devenvironment
sudo stop devenvironment
sudo restart devenvironment
share|improve this answer
    
Thanks Oli. Upstart looks interesting but the documentation didn't seem focused on my use case and I couldn't figure it out. I went for the quick and dirty bash solution in the end but I will definitely look into learning upstart as it looks very useful indeed. –  Rupert Madden-Abbott Apr 5 '11 at 22:00

Just a little tidbit that I use often. Instead of cd ~/path/to/my/working-directory, I typically use cd $(dirname $0). It will cd to the working directory that the script, $0, is in.

share|improve this answer

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.