0

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
qodeninja
  • 10,946
  • 30
  • 98
  • 152

5 Answers5

5

I found that using this will get the proper PID:

for /f "tokens=1,2,3,4 delims=/ " %%a in ('sc queryex ServiceName ^|FIND "PID"') do set PID=%%c
taskkill /pid %PID% /t /f

Then it works like a charm.

stealthyninja
  • 10,343
  • 11
  • 51
  • 59
Pierrick
  • 51
  • 1
  • 2
1

taskkill /fi "IMAGENAME eq idsm.exe" /fi "CPUTIME gt 00:30:00"

Image Name                     PID Session Name        Session#    Mem Usage
========================= ======== ================ =========== ============
idsm.exe                      4556 Console                    0     38,328 K

I got the this result, in the same command line how to kill this process, its should run automatically, means how to use this PID for killing

Josh Darnell
  • 11,304
  • 9
  • 38
  • 66
srinivas
  • 11
  • 1
1

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
  • 21,633
  • 5
  • 37
  • 59
  • 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 Dec 30 '09 at 22:01
1

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.
  • 28,486
  • 9
  • 71
  • 95
1

I found this the most reliable

net stop SERVICE_NAME
timeout /t 10
for /f "tokens=1,2,3,4 delims=/ " %%a in ('sc queryex SERVICE_NAME ^|FIND "PID"') do set PID=%%c
if not %PID% == 0 taskkill /pid %PID% /f
timeout /t 10
net start SERVICE_NAME

The timeout is arbitrary. The first timeout gives service a chance to stop itself properly for some extra amount of time. The second timeout allows the operating system to react, otherwise you may end up with

The service is starting or stopping. Please try again later.
and service will not restart.

I am checking if service is not stopped by if not %PID% == 0 and force killing it if necessary. The /t flag was unnecessary in my case.

interjaz
  • 193
  • 1
  • 1
  • 8