Posts tagged programing
Nginx Startup and Shutdown bat files with prompts and auto start at boot!
Jan 24th
So if you’ve read my previous post‘s I’m running the latest stable nginx server here on my local laptop to serve up my own search page 
or for when I’m blocking ad’s to have a page that fills in the void’s (and shows me just how well it does work!) 
But I left out a major point… You will need to open a command prompt and enter the C:\nginx directory and start nginx each and every time you bootup. That’s no fun even for a geek like me that spends half of his time on windows in an ssh terminal running back to one of my many linux pc’s but that’s still something we need to fix and like now!
So I did some digging around on google, and all of the post’s I’m reading say to use the .bat file located in the install, but the latest stable release of nginx DOESN’T INCLUDE THESE or ANY .bat FILES! Needless to say, that meant more work, and a quick intro into the quick grace at simple programming to have things done for you! So, Let’s build the start-nginx.bat file! (Note I’m running Windows 7 Ultimate Signature Edition, so this should work for you as well but let me know in the comment’s if it doesn’t!)
[codesyntax lang="winbatch" lines="normal" title="start-nginx.bat" blockstate="expanded"]
@ECHO OFF start nginx.exe echo Starting nginx EXIT
[/codesyntax]
We don’t need anything neat or pretty on the start up as we just want it to run with minimal interference to our start up routine (remember this will be automated later) Also note, that this .bat file needs to be in the C:\nginx folder along with nginx.exe for it to work properly!
Here’s the pimpish part, the shutdown .bat file. (I mostly just modified the file I found here) but it works great!
[codesyntax lang="winbatch" lines="normal" title="stop-nginx.bat" blockstate="expanded" highlight_lines="11"]
@echo off
echo Stopping Nginx…
:SETYESNO
set /p yesno= Would you like to kill nginx? [y/n]:
if “%yesno%”==”" (echo You didnt enter anything – try again..) & (GOTO SETYESNO)
if /I “%yesno%”==”n” (GOTO NOKILL)
if /I “%yesno%”==”y” (GOTO KILL) else (echo Unrecognized command – try again) & (GOTO SETYESNO)
:KILL
echo.
echo Killing Process…
nginx -s stop
GOTO QUIT
:NOKILL
echo.
echo Aborting kill…
:QUIT
echo.
pause
[/codesyntax]
There’s also an interesting idea on turning nginx into a window’s service if your into a bit more of a hacking mood then I was at 2:30 am writing this.
Also you can change the commands I’ve highlighted on line 11 with any one of the commands from the nginx site such as let nginx shutdown gracefully, reload itself etc. just remember that each line will execute in a command prompt as if you typed it so let your minds wander on this one! .bat files are your friend! (also as a side note if you don’t want to have to click any button to continue replace pause on line 18 with exit)
Now let’s get nginx running on our bootup. Right click the start-nginx.bat file and select “Create Shortcut”. Name it whatever you like and then right click the shortcut you just made and cut (or copy) the shortcut and let’s add it to our startup folder in the all programs list.
Click your start menu, choose all programs, scroll down to the startup folder, right click it and choose “Explore” or “Open” and paste your shortcut into this folder. (allow any prompt’s that windows throws at you for needing admin right’s) and you should be good to go!
If this worked for you or you have any questions that google can’t answer or just want to say thanks please feel free to post a comment! That’s why I do this!!! You’ll notice the lack of ad’s or shameless begging for donations, all I want is your thanks!

