0

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?

Community
  • 1
  • 1
Andersson
  • 47,234
  • 13
  • 52
  • 101

1 Answers1

1

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%
Compo
  • 11
  • 2