Forum

Windows batch File cleanup

warp9pnt9
16 March 2013, 15:56
Hiawatha version: 8.8.1
Operating System: Windows 7 Ultimate x64 + all current updates.

Currently, the .bat files used to start and test the webserver are a bit clunky.

1) One function per .bat file, multiple .bat files, more stuff to keep track of
2) No custom location
3) Runs in Command Prompt, doesn't utilize cynrunsrv.exe

To address these:

All I did was create one .bat file which will accept one argument. If invalid or no argument given, show usage.
The arguments are: install, remove, test, start and stop.

Install, Remove, Start and Stop call the included cygrunsrv.exe (great tool, thanks or including that).
Test calls the same things from TestConfig.bat (can now delete that file).

For my desired config, I have a Hiawatha folder which contains version numbers for folders, for easier install/upgrade/testing side by side. Not required.
But the .bat file has a VER, BIN and CFG environment variable to more easily change location. The BIN uses a windows style path, the CFG uses a cygwin style path.

Provided in the hope of being useful. It's not perfect (Usage message ECHO can't handle | without ugly quotes), but may be an improvement, or inspire something even better.

====[BEGIN: Hiawatha.bat]====
@ECHO OFF
REM
REM The Config file must be specified with a CygWin path.
REM
SET VER=8.8.1
SET BIN=C:\bin\Hiawatha\%VER%\bin
SET CFG=/cygdrive/c/bin/Hiawatha/%VER%/config

IF "%1" == "install" GOTO INSTALL
IF "%1" == "remove" GOTO REMOVE
IF "%1" == "test" GOTO TEST
IF "%1" == "start" GOTO START
IF "%1" == "stop" GOTO STOP

:USAGE
ECHO.
ECHO "Hiawatha.bat [install|remove|test|start|stop]"
ECHO.
ECHO install Install Hiawatha service.
ECHO remove Remove Hiawatha service.
ECHO test Test Hiawatha config.
ECHO start Start Hiawatha service.
ECHO stop Stop Hiawatha service.
GOTO END

:INSTALL
%BIN%\cygrunsrv.exe --install "Hiawatha" --type manual --disp "Hiawatha %VER%" --desc "Secure and advanced webserver" --path "%BIN%\hiawatha.exe" --args "-d -c %CFG%"
IF ERRORLEVEL 1 GOTO ERROR
ECHO Hiawatha service installed.
GOTO END

:REMOVE
%BIN%\cygrunsrv.exe --remove "Hiawatha"
IF ERRORLEVEL 1 GOTO ERROR
ECHO Hiawatha service removed.
GOTO END

:TEST
ECHO Hiawatha config test.

ECHO Wigwam:
"%BIN%\wigwam.exe" -c "%CFG%"
IF ERRORLEVEL 1 GOTO ERROR

ECHO.

ECHO Hiawatha:
"%BIN%\hiawatha.exe" -k -c "%CFG%"
IF ERRORLEVEL 1 GOTO ERROR

GOTO END

:START
%BIN%\cygrunsrv.exe --start "Hiawatha"
IF ERRORLEVEL 1 GOTO ERROR
ECHO Hiawatha service started.
GOTO END

:STOP
%BIN%\cygrunsrv.exe --stop "Hiawatha"
IF ERRORLEVEL 1 GOTO ERROR
ECHO Hiawatha service stopped.
GOTO END

:ERROR
ECHO Hiawatha error.

:END
SET VER=
SET BIN=
SET CFG=
====[END: Hiawatha.bat]====
warp9pnt9
16 March 2013, 16:10
Added a status option to run the cygrunsrv.exe --query. Now I feel this is more or less complete.

Modified bits to add to corresponding parts of .bat

IF "%1" == "status" GOTO STATUS

ECHO "Hiawatha.bat [install|remove|test|start|status|stop]"

ECHO status Get Hiawatha status.

:STATUS
ECHO Hiawatha service status.
ECHO.
%BIN%\cygrunsrv.exe --query "Hiawatha"
IF ERRORLEVEL 1 GOTO ERROR
GOTO END
Da JoJo
21 March 2013, 00:42
nice !
you can use this to elevate the commandprompt from within commandshell :

@if (1==1) @if(1==0) @ELSE
@echo off&SETLOCAL ENABLEEXTENSIONS
>nul 2>&1 "%SYSTEMROOT%\system32\cacls.exe" "%SYSTEMROOT%\system32\config\system"||(
cscript //E:JScript //nologo "%~f0"
@goto :EOF
)
echo.Engage Admin mode...
@goto :EOF
@end @ELSE
ShA=new ActiveXObject("Shell.Application")
ShA.ShellExecute("cmd.exe","/c \""+WScript.ScriptFullName+"\"","","runas",5);
@end
This topic has been closed.