forked from alibaba/arthas
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathas-service.bat
executable file
·281 lines (241 loc) · 7.56 KB
/
as-service.bat
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
@echo off
REM DON'T CHANGE THE FIRST LINE OF THE FILE, WINDOWS SERVICE RUN BAT NEED IT! (@echo off)
REM don't call 'echo' before 'start as.bat'
REM You can specify Java Home via AS_JAVA_HOME here or Windows System Environment, but not in cmd.exe
REM set AS_JAVA_HOME=C:\Program Files\Java\jdk1.8.0_131
set basedir=%~dp0
set filename=%~nx0
set srv_name=arthas
set telnet_port=3658
set http_port=8563
REM parse extend args
set arg1=%1
set pid=
set port=
set ignoreTools=0
set as_remove_srv=0
set as_service=0
set srv_interact=0
for %%a in (%*) do (
if "%%a"=="--remove" set as_remove_srv=1
if "%%a"=="--service" set as_service=1
if "%%a"=="--interact" set srv_interact=1
if "%%a"=="--ignore-tools" set ignoreTools=1
)
REM Parse command line args (https://stackoverflow.com/a/35445653)
:read_params
if not %1/==/ (
if not "%__var%"=="" (
if not "%__var:~0,1%"=="-" (
endlocal
goto read_params
)
endlocal & set %__var:~1%=%~1
) else (
setlocal & set __var=%~1
)
shift
goto read_params
)
if not "%telnet-port%"=="" set telnet_port=%telnet-port%
if not "%http-port%"=="" set http_port=%http-port%
REM Setup JAVA_HOME
REM Decode -java-home: '@' -> ' '
if not "%java-home%"=="" set JAVA_HOME=%java-home:@= %
REM If has AS_JAVA_HOME, overriding JAVA_HOME
if not "%AS_JAVA_HOME%" == "" set JAVA_HOME=%AS_JAVA_HOME%
REM use defined is better then "%var%" == "", avoid trouble of ""
if not defined JAVA_HOME goto noJavaHome
REM Remove "" in path
set JAVA_HOME=%JAVA_HOME:"=%
if not exist "%JAVA_HOME%\bin\java.exe" goto noJavaHome
if %ignoreTools% == 1 (
echo Ignore tools.jar, make sure the java version ^>^= 9
) else (
if not exist "%JAVA_HOME%\lib\tools.jar" (
echo Can not find lib\tools.jar under %JAVA_HOME%!
echo If java version ^<^= 1.8, please make sure JAVA_HOME point to a JDK not a JRE.
echo If java version ^>^= 9, try to run %filename% ^<pid^> --ignore-tools
goto :end
)
)
set JAVACMD="%JAVA_HOME%\bin\java"
REM Runas Service, don't call 'echo' before 'start as.bat'
set as_args=-telnet-port %telnet_port% -http-port %http_port%
if %srv_interact%==0 set as_args=%as_args% --no-interact
if %ignoreTools%==1 set as_args=%as_args% --ignore-tools
if %as_service%==1 (
REM run as.bat
start /wait %basedir%\as.bat %pid% %as_args%
exit 0
REM DEBUG run args
REM echo as_args: %as_args%
REM echo start /wait %basedir%\as.bat %pid% %as_args%
REM exit /b 0
)
REM If the first arg is a number, then set it as pid
echo %arg1%| findstr /r "^[1-9][0-9]*$">nul
if %errorlevel% equ 0 set pid=%arg1%
echo pid: %pid%
echo port: %port%
if not ["%pid%"] == [""] (
goto :prepare_srv
)
if not ["%port%"] == [""] (
goto :find_port
)
if %as_remove_srv%==1 (
goto :remove_srv
)
goto :usage
:remove_srv
echo Removing service: %srv_name% ...
sc stop %srv_name%
sc delete %srv_name%
exit /b 0
:find_port
netstat -nao |findstr LIST |findstr :%telnet_port%
IF %ERRORLEVEL% EQU 0 (
echo Arthas agent already running, just connect to it!
goto :attachSuccess
)
@rem find pid by port
echo %port%| findstr /r "^[1-9][0-9]*$">nul
if %errorlevel% neq 0 (
echo port is not valid number!
goto :usage
)
echo Finding process of listening on port: %port%
set query_pid_command='netstat -ano ^^^| findstr ":%port%" ^^^| findstr "LISTENING"'
set pid=
for /f "tokens=5" %%i in (%query_pid_command%) do (
set pid=%%i
)
if "%pid%" == "" (
echo None process listening on port: %port%
goto :end
)
echo Target process pid is %pid%
:prepare_srv
REM check telnet port
netstat -nao |findstr LIST |findstr :%telnet_port%
IF %ERRORLEVEL% EQU 0 (
echo Arthas agent already running, just connect to it!
goto :attachSuccess
)
REM validate pid
echo %pid%| findstr /r "^[1-9][0-9]*$">nul
if %errorlevel% neq 0 (
echo PID is not valid number!
goto :usage
)
echo Preparing arthas service and injecting arthas agent to process: %pid% ...
REM encode java path, avoid space in service args: ' ' -> '@'
set srv_java_home=-java-home %JAVA_HOME: =@%
set srv_args=-pid %pid% -telnet-port %telnet_port% -http-port %http_port% %srv_java_home%
if %srv_interact%==1 (
sc start UI0Detect
set srv_type=type= interact type= own
set srv_binpath=binPath= "%basedir%\%filename% %srv_args% --service --interact"
) else (
set srv_type=type= own
set srv_binpath=binPath= "%basedir%\%filename% %srv_args% --service"
)
echo arthas srv type: %srv_type%
echo arthas srv binPath: %srv_binpath%
sc create %srv_name% start= demand %srv_type% %srv_binpath%
sc config %srv_name% start= demand %srv_type% %srv_binpath%
if %errorlevel% NEQ 0 (
echo Config Arthas service failed
exit /b -1
)
sc stop %srv_name%
REM fork start Arthas service, avoid blocking
if %srv_interact%==1 (
start /B sc start %srv_name%
)else (
start /B sc start %srv_name% > nul 2>&1
)
REM check and connect arthas ..
echo Waitting for arthas agent ...
set count=0
:waitfor_loop
echo checking
netstat -nao |findstr LIST |findstr :%telnet_port%
IF %ERRORLEVEL% NEQ 0 (
set /a count+=1
if %count% geq 8 (
echo Arthas agent telnet port is not ready, maybe inject failed.
goto :end
)
ping -w 1 -n 2 0.0.0.0 > nul
goto :waitfor_loop
)
echo Arthas agent telnet port is ready.
:attachSuccess
WHERE telnet
IF %ERRORLEVEL% NEQ 0 (
ECHO telnet wasn't found, please google how to install telnet under windows.
ECHO Try to visit http://127.0.0.1:%http_port% to connecto arthas server.
start http://127.0.0.1:%http_port%
) else (
telnet 127.0.0.1 %telnet_port%
)
echo(
echo Checking arthas telnet port [:%telnet_port%] ...
netstat -nao |findstr LIST |findstr :%telnet_port%
IF %ERRORLEVEL% EQU 0 (
echo Arthas agent is still running!
goto :choice
) else (
echo Arthas agent is shutdown.
goto :end
)
:choice
set /P c=Are you going to shutdown arthas agent [Y/N]?
echo input: %c%
if /I "%c%" EQU "Y" goto :shutdown_agent
if /I "%c%" EQU "N" goto :end
goto :choice
:shutdown_agent
echo Shutting down arthas ...
%JAVACMD% -jar arthas-client.jar -c shutdown 127.0.0.1 %telnet_port%
@rem check telnet port agian
echo Checking arthas telnet port [:%telnet_port%] ...
netstat -nao |findstr LIST |findstr :%telnet_port%
IF %ERRORLEVEL% EQU 0 (
echo Arthas shutdown failed!
) else (
echo Arthas shutdown successfully.
)
goto :end
:usage
echo Arthas for Windows Service.
echo Usage:
echo %filename% java_pid [option] ..
echo %filename% [-pid java_pid] [option] ..
echo %filename% [-port java_port] [option] ..
echo(
echo Options:
echo -pid java_pid : Attach by java process pid
echo -port java_port : Attach by java process listen port
echo -telnet-port port : Change arthas telnet port
echo -http-port port : Change arthas http/websocket port
echo --interact : Enable windows service interactive UI, useful for debug
echo --remove : Remove Arthas windows service
echo --ignore-tools : Ignore checking JAVA_HOME\lib\tools.jar for jdk 9/10/11
echo(
echo Example:
echo %filename% 2351
echo %filename% 2351 -telnet-port 2000 -http-port 2001
echo %filename% -pid 2351
echo %filename% -port 8080 --interact
echo %filename% --remove #remove arthas service
exit /b -1
:noJavaHome
echo JAVA_HOME: %JAVA_HOME%
echo The JAVA_HOME environment variable is not defined correctly.
echo It is needed to run this program.
echo NB: JAVA_HOME should point to a JDK not a JRE.
exit /b -1
:end