简体   繁体   中英

Code 255 due to unexpected command

I'm trying to kill process on 6373 port (as suggested here ) with following code:

FOR /F "tokens=5 delims= " %P IN ('netstat -a -n -o ^| findstr :6373') DO TaskKill.exe /F /PID %P

and this works if I run it from cmd on local machine...

But got

6373') was unexpected at this time.
Process exited with code 255

if I try to run it as build-step on TeamCity Windows agent

I tried tokens=4 instead of tokens=5 as well as %%P instead of %P , but got same result. Can anyone point on my mistake?

The code can be rewritten to remove the uncertainty of an unknown number of tokens.

Single line from console window:

@Set "PID=" & @(For /F "Tokens=*" %a In ('NetStat -a -n -o^|Find ":6373 "') Do @For %b In (%a) Do @Set PID=%b) & @If Defined PID TaskKill /F /PID %PID%

Batch file version:

@Echo Off
SetLocal
Set "PID="
For /F "Tokens=*" %%a In ('NetStat -a -n -o^|Find ":6373 "') Do (
    For %%b In (%%a) Do Set PID=%%b
)
If Defined PID TaskKill /F /PID %PID%

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM