Welcome! Log In Create A New Profile

Advanced

Windows 7 - start NGINX at boot - task and batch script

Posted by iryim 
Windows 7 - start NGINX at boot - task and batch script
June 24, 2015 09:52AM
I wrestled for a while trying to get NGINX to cleanly start at system boot (Win7) regardless of the last status of NGINX before the system shutdown. In case anyone else might find this useful, I thought I'd post them up. I'll attach the files, but I'll add the text below so that everyone can see what I'm doing here. (Note: the "Author" info in the task has been sanitized, so you'll want to change it to something valid for your evnironment.) (Another Note: The attached "check-nginx.txt" file is actually the .bat file. I had to change the extension to upload it.)

-----------------------------

The Task:

<?xml version="1.0" encoding="UTF-16"?>
<Task version="1.3" xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task">
<RegistrationInfo>
<Date>2015-05-26T14:41:35.6523561</Date>
<Author>LOCALHOST\localuser</Author>
</RegistrationInfo>
<Triggers>
<BootTrigger>
<Enabled>true</Enabled>
<Delay>PT5M</Delay>
</BootTrigger>
</Triggers>
<Principals>
<Principal id="Author">
<UserId>S-1-5-18</UserId>
<RunLevel>HighestAvailable</RunLevel>
</Principal>
</Principals>
<Settings>
<MultipleInstancesPolicy>StopExisting</MultipleInstancesPolicy>
<DisallowStartIfOnBatteries>false</DisallowStartIfOnBatteries>
<StopIfGoingOnBatteries>true</StopIfGoingOnBatteries>
<AllowHardTerminate>true</AllowHardTerminate>
<StartWhenAvailable>false</StartWhenAvailable>
<RunOnlyIfNetworkAvailable>true</RunOnlyIfNetworkAvailable>
<IdleSettings>
<StopOnIdleEnd>true</StopOnIdleEnd>
<RestartOnIdle>false</RestartOnIdle>
</IdleSettings>
<AllowStartOnDemand>true</AllowStartOnDemand>
<Enabled>true</Enabled>
<Hidden>false</Hidden>
<RunOnlyIfIdle>false</RunOnlyIfIdle>
<DisallowStartOnRemoteAppSession>false</DisallowStartOnRemoteAppSession>
<UseUnifiedSchedulingEngine>false</UseUnifiedSchedulingEngine>
<WakeToRun>false</WakeToRun>
<ExecutionTimeLimit>PT0S</ExecutionTimeLimit>
<Priority>7</Priority>
</Settings>
<Actions Context="Author">
<Exec>
<Command>C:\nginx-1.9.0\check-nginx.bat</Command>
<WorkingDirectory>C:\nginx-1.9.0\</WorkingDirectory>
</Exec>
</Actions>
</Task>

------------------------------

The batch:

@echo off

REM Set local cmd environment
REM

SETLOCAL EnableExtensions

REM Set variable "EXE" to value "nginx"
REM

set EXE=nginx.exe

REM Look for nginx in tasklist
REM if running, skip to reloading the program "RUNNING" below
REM

FOR /F %%x IN ('tasklist /NH /FI "IMAGENAME eq %EXE%"') DO IF %%x == %EXE% goto RUNNING

REM If NGINX isn't running, say so
REM

echo NGINX not running

REM Look for stale PID file, delete it if found and say so
REM Start NGINX

IF EXIST c:\nginx-1.9.0\logs\nginx.pid (
ECHO Deleting PID file and starting NGINX
DEL c:\nginx-1.9.0\logs\nginx.pid
start nginx
) ELSE (
ECHO Starting NGINX Fresh
start nginx
)

goto FIN
:RUNNING
REM If NGINX is running, say so and reload
REM

echo NGINX is running - reloading
nginx -s reload

:FIN

REM For debugging, you can insert a pause here to see the echoed
REM messages and see which path was taken.
Attachments:
open | download - check-nginx.txt (928 bytes)
open | download - Start-NGINX-sanitized.xml (1.6 KB)
Sorry, only registered users may post in this forum.

Click here to login

Online Users

Guests: 123
Record Number of Users: 8 on April 13, 2023
Record Number of Guests: 500 on July 15, 2024
Powered by nginx      Powered by FreeBSD      PHP Powered      Powered by MariaDB      ipv6 ready