bat启停脚本

1.启动scratch和node(node可能有两个版本)

@echo off

rem start nginx begin
set nginxPid=-1
if exist ./nginx-1.14.0/logs/nginx.pid (
	for /f "tokens=* delims=" %%i in (./nginx-1.14.0/logs/nginx.pid) do set nginxPid=%%i
)
echo nginx pid:%nginxPid%

set needStartNginx=1
if %nginxPid% NEQ -1 (
	for /f "tokens=1" %%i in ('tasklist ^| findstr %nginxPid%') do (
		if '%%i' == 'nginx.exe' (
			set needStartNginx=0
		)
	)
)

if %needStartNginx% == 1 (
	echo "start nginx begin!"
	cd nginx-1.14.0
	start nginx.exe
	cd ..
	echo "start nginx end!"
) else (
	echo "nginx already start!"
)
rem start nginx end


rem start scratch begin 
set scratchPid=-1
for /f "tokens=5" %%i in ('netstat -ano ^| findstr ":3000"') do (
    set scratchPid=%%i
)

if %scratchPid% == -1 (
	echo "start scratch begin!"
	node6.exe Main.js
	echo "start scratch end!"
) else (
	for /f "tokens=1" %%i in ('tasklist ^| findstr %scratchPid%') do (
		if '%%i' == 'node6.exe' (
			echo "scratch already start"
		) else (
			if '%%i' == 'node.exe' (
				echo "scratch already start"
			) else (
				echo "scratch port(3000) is used by other process."
				echo "start stop %%i begin!"
				taskkill /pid %%i -f
				echo "start stop %%i end!"
				echo "start scratch begin!"
				node6.exe Main.js
				echo "start scratch end!"
			)
		)
	)
)
rem start scratch end

2.停止nginx和node脚本

@echo off

rem stop nginx begin
set nginxPid=-1
if exist ./nginx-1.14.0/logs/nginx.pid (
	for /f "tokens=* delims=" %%i in (./nginx-1.14.0/logs/nginx.pid) do set nginxPid=%%i
)
echo nginx pid:%nginxPid%

if %nginxPid% == -1 (
	echo "nginx already stop!"
) else (
	for /f "tokens=1" %%i in ('tasklist ^| findstr %nginxPid%') do (
		if '%%i' == 'nginx.exe' (
			echo "stop nginx begin"
			cd nginx-1.14.0
			start nginx.exe -s quit
			cd ..
			echo "stop nginx end"
		)
	)
)
rem stop nginx end

rem stop scratch begin
set scratchPid=-1
for /f "tokens=5" %%i in ('netstat -ano ^| findstr ":3000"') do (
    set scratchPid=%%i
)

if %scratchPid% == -1 (
	echo "scratch already stop!"
) else (
	for /f "tokens=1" %%i in ('tasklist ^| findstr %scratchPid%') do (
		if '%%i' == 'node6.exe' (
			echo "stop scratch begin!"
			taskkill -f /pid %scratchPid%
			echo "stop scratch end!"
		) else (
			if '%%i' == 'node.exe' (
				echo "stop scratch begin!"
				taskkill -f /pid %scratchPid%
				echo "stop scratch end!"
			) else (
				echo "scratch already stop!"
			)
		)
	)
)
rem stop scratch end

你可能感兴趣的:(运维)