首页 » 工作 » 正文

封装svr.bat!管理nginx+php+windows服务器

发布者:站点默认
2011/04/30 浏览数(2,362) 分类:工作 封装svr.bat!管理nginx+php+windows服务器已关闭评论

说明:

  使用bat封装了一些管理windows、php、nginx服务器的常用命令,可以通过简单的命令来启动、重启、退出nginx和php组合,详细用法见下文。在文末upall为大家准备了一个php-cgi.exe的守护windows批处理脚本,用于php-cgi.exe自动退出后重新启动php-cgi程序。

预览:

win2k3+nginx+php-cgi

win2k3+nginx+php-cgi

用法:

svr.bat  [run | rerun | bye | ste | help]

例子:
  启动服务:svr.bat run(启动nginx和php、“.bat”可省略,下同)
  退出服务:svr bye
  重启服务:svr rerun
  查看状态:svr ste
  仅启动php:svr php run // 关闭:bye,重启:rerun(不能查看运行状态),下同
  仅启动nginx:svr nginx run

svr.bat代码:

:: 作者:upall,http://upall.cn/
:: 作用:管理nginx服务器
:: 咋用:
::   启动:svr run
::   状态:svr ste
::   重启:svr rerun
::   退出:svr bye
::   帮助:svr help
:: 日期:23:47 2011年5月2日
@echo off
setlocal
set nginxFolder=D:\Svr\nginx\nginx-1.0.0
set phpFolder=D:\Svr\php\php-5.3.6-nts-Win32-VC9-x86

if /i "help"=="%1" goto help
if /i "ste"=="%1" goto status
if /i "run"=="%1" goto run
if /i "rerun"=="%1" goto rerun
if /i "bye"=="%1" goto bye
if /i "php"=="%1" goto php
if /i "nginx"=="%1" goto nginx
if /i "cls"=="%1" cls
if /i "ls"=="%1" dir %2 %3 %4 %5 %6 %7 %8 %9 %10 %11 %12 %13 %14 %15 %16 %17
goto end

:status
tasklist | find /i "php-cgi.exe" 1>nul 2>nul
if "0"=="%errorlevel%" (
	netstat -ano | find "9000" 1>nul 2>nul
	if "0"=="%errorlevel%" (
		echo  php-cgi is running ....
	) else (
		echo  php-cgi is running, but it cann't to work ....
	)
) else (
	echo  php-cgi is stoped.
)
tasklist | find /i "nginx.exe" 1>nul 2>nul
if "0"=="%errorlevel%" (
	netstat -ano | find "80" 1>nul 2>nul
	if "0"=="%errorlevel%" (
		echo  nginx is running ....
	) else (
		echo  nginx is running, but it cann't to work ....
	)
) else (
	echo  nginx is stoped.
)

goto end
:run
::check
tasklist | find /i "php-cgi.exe" 1>nul 2>nul
if "0"=="%errorlevel%" taskkill /f /im php-cgi.exe 1>nul 2>nul
tasklist | find /i "nginx.exe"   1>nul 2>nul
if "0"=="%errorlevel%" taskkill /f /im nginx.exe   1>nul 2>nul
::php
echo  start php-cgi ....
cd /d "%phpFolder%"
start /min "php-cgi is Running ...." %phpFolder%\php-cgi.exe -b 127.0.0.1:9000 -c %phpFolder%\php.ini
::nginx
echo  start nginx ....
cd /d "%nginxFolder%"
start /min "Nginx is Running ...." cmd /k "nginx.exe && exit"
goto end

:rerun
echo  quit nginx ....
cd /d "%nginxFolder%"
start /min "" cmd /k "nginx.exe -s quit && exit"
echo  kill php-cgi ....
taskkill /f /im php-cgi.exe 1>nul 2>nul
goto run

:bye
echo  quit nginx ....
cd /d "%nginxFolder%"
start /min "" nginx.exe -s quit
echo  kill php-cgi.exe ....
taskkill /f /im php-cgi.exe 1>nul 2>nul
goto end

:php
if "run"=="%2" goto phpRun
if "bye"=="%2" goto phpQuit
if "rerun"=="%2" goto phprerun
goto end
:phprerun
echo  kill php-cgi.exe ....
taskkill /f /im php-cgi.exe 1>nul 2>nul
:phpRun
echo  start php-cgi ....
cd /d "%phpFolder%"
start /min "php-cgi is Running ...." %phpFolder%\php-cgi.exe -b 127.0.0.1:9000 -c %phpFolder%\php.ini
goto end
:phpQuit
echo  kill php-cgi.exe ....
taskkill /f /im php-cgi.exe 1>nul 2>nul
goto end

:nginx
if "run"=="%2" goto nginxRun
if "bye"=="%2" goto nginxQuit
if "rerun"=="%2" goto nginxrerun
goto end
:nginxrerun
echo  quit nginx ....
cd /d "%nginxFolder%"
start /min "" cmd /k "nginx.exe -s quit && exit"
:nginxRun
echo  start nginx ....
cd /d "%nginxFolder%"
start /min "Nginx is Running ...." cmd /k "nginx.exe && exit"
goto end
:nginxQuit
echo  quit nginx ....
cd /d "%nginxFolder%"
start /min "" nginx.exe -s quit
goto end

:help
echo  《Nginx+PHP服务器管理工具》 网址:http://upall.cn/
echo  用法:
echo    svr.bat  [run ^| rerun ^| bye ^| ste ^| help]
goto end

:end
endlocal

补充:据说php-cgi.exe有生命周期,会自动退出,这个是守护脚本php-cgi-nurse.bat代码,脚本每4秒检查一次php-cgi.exe程序是否存在,并且运行是否正常,不正常会自己启动新的php-cgi程序:

:: 作用:php-cgi 守候脚本
:: 作者:upall,http://upall.cn/
@echo off
setlocal
title php-cgi 守护脚本
set logFile=D:\Logs\php-cgi.log
:begin
ping 127.0.0.1 -n 4 >nul
tasklist | find /i "php-cgi.exe" 1>nul 2>nul
if "0"=="%errorlevel%" (
	netstat -ano | find "9000" 1>nul 2>nul
	if "0"=="%errorlevel%" (
		echo [%date:~0,10% %time:~0,8%] php-cgi is running ....
	) else (
		echo  php-cgi is running, but it cann't to work ....
      :: 如果出错就写日志
      echo [%date% %time%] [notice] php-cgi is running, but it cann't to work ....>>%logFile%
      :: 重新启动php-cgi,需配合上文中的svr.bat
      start /min "" cmd /k "svr.bat php rerun && exit"
	)
) else (
	echo [%date% %time%] php-cgi is stoped.
	:: 如果出错就写日志
	echo [%date% %time%] [error] php-cgi is stoped.>>%logFile%
	:: 启动php-cgi,需配合上文中的svr.bat
	start /min "" cmd /k "svr.bat php run && exit"
)
goto begin

<完>

点击返回顶部
  1. 留言
  2. 联系方式