7

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.

1
  • 1
    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.
    – naisanza
    Oct 29, 2013 at 21:04

2 Answers 2

9

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
2
  • 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 :) Apr 5, 2011 at 21:59
  • @Rupert me too, lol. And I used today!
    – H_7
    Mar 25, 2012 at 1:11
4

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
1
  • 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. Apr 5, 2011 at 22:00

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .