1

I'm trying to create a bat file on windows 10 to start the nginx and php-fpm in background. At the moment I can do that but the command window (black window) remains open. If I close the window the php-fpm is terminated as well.

here is what I've added in start.bat

@ECHO OFF
cd D:\servers\nginx-1.10.1\
echo Starting Nginx...
start /B cmd /K "D:\servers\nginx-1.10.1\nginx.exe"
echo Start Php-cgi...
start /B cmd /K "D:\servers\php-5.6.26\php-cgi.exe -b 127.0.0.1:9000 -c D:\servers\php-5.6.26\php.ini"
EXIT

this is what is printed on black window:

Starting Nginx...
Start Php-cgi...

I want the window to automatically close, but keep the process(nginx and php-fpm) running.

0

3 Answers 3

3

php-cgi locks the window. Try the solution nginx guys give on their page. PHP-FastCGI on Windows

0

You can hide console window as one of the solutions to this problem. It's easy to do with CLI utilities like hidcon. For my project, I use this script: start-server.cmd

@Echo Off
:: Portable nginx+php5+mysql for Windows
:: Inquisitor, 2016-2018
CD /D "%~dp0"

:: Settings
Set LogFile=.\%~n0.log
Set TimestampFormat=[%%Date%% %%Time:~,8%%]
Set WatcherTimeout=10

If "%~1"=="" (
    Start "" "%~dp0tools\hidcon" %~nx0 restart_hidden
    Call Echo.>>"%LogFile%"
    Call :Log Loader started
    Exit
)


Set Path=%~dp0tools;%~dp0nginx;%~dp0php-fpm;%~dp0mysql\bin;%Path%
::  ImageMagick 6.9.3-3
rem Set Path=%Path%;%~dp0modules\imagick6
::  ImageMagick 7.0.5.10
rem Set Path=%Path%;%~dp0modules\imagick7
::  FFMpeg 3.3.1
Set Path=%Path%;%~dp0modules\ffmpeg

Set Temp=%~dp0\tmp



:: MySQL
Call :Log Starting MySQL...
:start-mysql
PushD .\mysql\bin
Start mysqld --defaults-file=my.ini --standalone
PopD
If "%1"=="restart" (
    Call :Log MySQL crashed, restarting
    Exit /B
)

:: PHP
Call :Log Starting PHP-FPM...
:start-php
PushD .\php-fpm
Start hidcon php-cgi -b 127.0.0.1:9123 -c ".\php.ini"
PopD
If "%1"=="restart" (
    Call :Log PHP-FPM crashed, restarting
    Exit /B
)

:: nginx
Call :Log Starting NginX...
:start-nginx
PushD .\nginx
Start nginx
PopD
If "%1"=="restart" (
    Call :Log NginX crashed, restarting
    Exit /B
)



TaskKill /F /IM "hidcon.exe"

For %%A In ("mysqld.exe", "php-cgi.exe", "nginx.exe") Do (
    wmic process where name="%%~A" CALL setpriority "high priority"
)



Set LockFile=.\.%~n0.lock
Echo.>"%LockFile%"

:Watcher
Ping -n %WatcherTimeout% 127.0.0.1>nul 2>nul
TaskList /FI "imagename EQ php-cgi.exe" /FO:CSV|FindStr /I /C:"php-cgi.exe">nul||Call :start-php restart
If Not Exist "%LockFile%" (
    Call :Log Lockfile not found, starting shutdown process

    PushD .\nginx
    nginx -s stop
    PopD

    mysqladmin -u root -pPCIIctvYUijcfNLR shutdown

    TaskKill /F /T /IM php-cgi.exe

    Call :Log Loader stopped
    Exit
)
GoTo :Watcher

:Log
    Call Echo %TimestampFormat% %*>>"%LogFile%"
Exit /B

and simple stop-server.cmd:

@Echo Off
Del "%~dp0.loader.lock" 2>nul
0
RunHiddenConsole.exe php-cgi.exe -b 127.0.0.1:9001

https://github.com/wenshui2008/RunHiddenConsole

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .