views:

499

answers:

2

I'm capturing the PID in a variable which I kill later

  IF NOT "%SERVICE_PID%" == 0 taskkill /pid %SERVICE_PID% /t /f 

though everytime I do this in a batch file it makes my computer restart because it kills some system process

the service pid should be a user defined service launched from cmd

I dont understand why it keeps making my machine croak.

When I run "taskkill /pid %SERVICE_PID% /t /f" on the command line it works fine! =/

help!

Setting SERVICE_PID

FOR /F "tokens=4 delims= " %%A IN ('sc queryex myservice ^|FIND "PID"')
 DO SET SERVICE_PID=%%A
+1  A: 

Try removing /f option, it forces to terminate a process, so system processes might get terminated forcefully without notification.

I am suspecting you are trying to kill some system processes in the batch script, in the sense that in your list of PIDs there might be some system process IDs as well.

Thanks

Mahesh Velaga
If SERVICE_PID contains wrong values, removing /f will not be a solution (though it helps to avoid a system crash). If SERVICE_PID contains the right value, using /f should do no harm.
Doc Brown
+1  A: 

Make sure you have enabled delayed expansion. See the HELP SET for an explanation.

Insert this line

SETLOCAL ENABLEDELAYEDEXPANSION

as the first line of your batch file.

and use !SERVICE_PID! instead of %SERVICE_PID% to get the environment variable.

PA