Starting and Stopping More Efficiently

Like most of you, I hope, I don't just write code and throw it up on a production server and see if it works.  Rather after you've written it, you run it on a development platform that mimics your production server relatively closely.  At work, we have a development server that is basically a scaled down version of our production server.  So when writing code, I upload to it and run it there.

For my personal development however, I develop on my desktop (and occasionally my laptop).  My desktop is powered by Windows 7 Ultimate 64-bit and I have Apache, ColdFusion, PostgreSQL, and MySQL installed.  And of course, I do all of my coding in Dreamweaver CS 5.5. 

Now my desktop is a pretty good little system, a slightly aged gaming Dell XPS, but I only have a 4 GB of RAM.  So I don't want my web development software running all the time.  I have long used some little scripts I wrote to start up the programs I generally run when I first sit down to work at my systems.  But they've only started programs and I didn't have anything to shut them back down.

So finally decided to learn how to make the sort of scripts I wanted to run when I sit down to start some web development and when I'm done for the day.  Now this trick may be something a lot of you are familiar with, but if you aren't then I hope it helps you in your own personal development and other computing endeavors.

So to do this, you basically write your own batch files.  This is a simple text document that gives your OS a series of commands to run when executed.  Now that files can get really complicated as you can do a lot with them but today were going to just do some simple stuff.  Since they are plain text files, you can create and edit them in Notepad.

So first, my web development start file.  When I'm doing my web development, I need the aforementioned Apache, ColdFusion, my databases, Dreamweaver, as well as Windows Explorer and Firefox.  For the curious, I actually use Chrome as my primary browser at home however Firefox's Web Developer Toolbar is way better than the one they putting Chrome so I use Firefox when I'm doing development.

For starting programs, use the start command.  Some core Windows programs, like Explorer can simply be started by saying start explorer.exe.  You can tell it what folder to start in by adding the path after the command.

start explorer.exe I:\ColdFusion9\wwwroot

For other programs, you still use the start command, however you have to give it the full path to where it can find the executable.  Since Windows folders allow spaces in the name, you'll also need to quote the path.

start /d"C:\Program Files (x86)\Mozilla Firefox\" firefox.exe

For services, you use the net start command, followed by the service name in quotes.  If you'd like each service to have its own separate command window, you start net start.

To get the name of the service:

  1. open your services panel
  2. find the service you want to have started
  3. right-click it and open the properties
  4. the name to use appears beside the service name label, so you can just highlight it and copy it

net start "Apache2.2"

That's pretty much it.  You one command per line, add all the ones you want, and then save the file with whatever name you want in a .bat extension.  If your system can handle all of your commands running at the same time, or you need to cascade them a bit, add a sleep command to one line, followed by the number of seconds you want the system to wait for executing the next command.

So here is my final start up file:

start explorer.exe I:\ColdFusion9\wwwroot
start /d"C:\Program Files (x86)\Mozilla Firefox\" firefox.exe
start /d"C:\Program Files (x86)\Adobe\Adobe Dreamweaver CS5.5\" Dreamweaver.exe
net start "Apache2.2"
net start "ColdFusion 9 Application Server"
net start "postgresql-x64-9.2"

Double-click the batch file to test that out.  Once I have my batch file ready to go, I'll usually make a shortcut to it from my desktop and then give it a descriptive name and whatever icon looks appropriate for what I'm using it for.

Then comes the batch file for shutting down everything.  For the services, we'll use a similar command that we use to start it, except instead of net start we say net stop.

net stop "Apache2.2"

To shut down programs, you use the taskkill you'll command.

taskkill /IM firefox.exe

Shutting down the programs through taskkill all is a little more tricky and I generally avoid using it altogether.  Some programs, like Dreamweaver and Firefox, it works perfectly fine for.  But you generally don't want to do with something like oh Windows Explorer or other system functions that may actually have multiple processes running.

So for me, this ended up being my shut down file:

net stop "Apache2.2"
net stop "ColdFusion 9 Application Server"
net stop "postgresql-x64-9.2"
taskkill /IM firefox.exe
taskkill /IM Dreamweaver.exe

On the whole, I'm pretty happy with the results.  It certainly much easier for me to double-click each icon versus having to go to the start menu and click my programs then go to services and right click start multiple times.  Much less having to go undo everything when I'm done!

If I could make any improvement to the two scripts, it would be to figure out how to tell my startup script to automatically update from my SVN repository for each of my development sites and tell my shutdown script to commit to my SVN repository when I'm done.  I'm fairly certain it is possible, since I used TortoiseSVN which has command line options.  I just haven't explored it yet. 🙂

If you have and you can give me some tips, feel free to share!