Skip to content

Instantly share code, notes, and snippets.

@mkoertgen
Last active April 19, 2016 12:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mkoertgen/c7529356b4c006beb2d9d26112ab57c5 to your computer and use it in GitHub Desktop.
Save mkoertgen/c7529356b4c006beb2d9d26112ab57c5 to your computer and use it in GitHub Desktop.
Using built-in mkdocs server in production on Windows (Scheduled Task)
@echo off
setlocal
set logfile=%~n0.log
set taskName=%~n0
cd /D "%~dp0"
set command=%1
if "%command%"=="" set command=usage
goto %command%
:usage
echo Local documentation server using mkdocs.
echo.
echo Usage: %~n0 [command]
echo.
echo Available commands:
echo.
echo usage Shows this help
echo run Runs the documentation server (console)
echo install Installs the documentation server as scheduled task
echo start Starts the installed server (task)
echo stop Stops the installed server (task)
echo restart Restarts the installed server (task)
echo uninstall Uninstalls the documentation server
goto end
:install
echo Installing mkdocs server...
echo.
schtasks /Create /TN %taskName% /RU SYSTEM /SC onstart /TR "%~f0 run"
call :check_error %errorlevel% "Could not install mkdocs server task." || exit /B 1
echo.
call :start || exit /B 2
echo.
echo Installed mkdocs server...
goto end
:uninstall
echo Uninstalling mkdocs server...
echo.
call :stop || exit /B 2
echo.
schtasks /Delete /TN %taskName% /f
call :check_error %errorlevel% "Could uninstall mkdocs server." || exit /B 1
del "%logfile%" 2>nul
echo.
echo Uninstalled mkdocs server...
goto end
:start
echo Starting mkdocs server...
schtasks /Run /I /TN %taskName%
call :check_error %errorlevel% "Could not start mkdocs server." || exit /B 1
echo Started mkdocs server (logs: '%logfile%').
goto end
:stop
echo Stopping mkdocs server...
schtasks /End /TN %taskName%
call :check_error %errorlevel% "Could not stop mkdocs server." || exit /B 1
taskkill /im mkdocs.exe /f 2>nul
rem wait 2 seconds to relieve access problems to logfile
ping 127.0.0.1 -n 2 > nul
echo Stopped mkdocs server.
echo. >>%logfile%
echo Stopped mkdocs server (%DATE% %TIME%). >>%logfile%
goto end
:restart
echo Restarting mkdocs server...
echo.
call :stop || exit /B 1
echo.
call :start || exit /B 2
echo.
echo Restarted mkdocs server.
echo Restarted mkdocs server. >>%logfile%
goto end
:run
echo Starting mkdocs server...
echo Please keep this window open (minimized is OK).
echo Press Ctrl+C in case you need to stop.
echo Starting mkdocs server (%DATE% %TIME%)... >%logfile%
echo. >>%logfile%
mkdocs serve >>%logfile% 2>&1
rem exit code 1 is Ok (Ctrl+C)
call :check_error %errorlevel% "Could not run mkdocs server." 1 log || exit /B 1
goto end
:end
endlocal
exit /B 0
:check_error
set /a exitCode = %1
set /a okCode = 0
if not "%3"=="" set /a okCode = %3
if %exitCode% equ %okCode% exit /B 0
echo ERROR: %2 (code: %exitCode%)
if "%4"=="log" echo ERROR: %2 (code: %exitCode%) >>%logfile%
exit /B %exitCode%
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment