Killing a process in Batch and reporting on success
Asked Answered
L

3

5

i have the following batch file, which terminates the iTunes program so, that if i connect my iPod, it's not going to sync it. (I know you can set this up in iTunes.)

@echo off
:kill
cls
taskkill /F /IM itunes.exe >nul
if %errorlevel%==1 {
echo iTunes not found.
} else {
echo iTunes is killed.
}
goto kill

However, the >nul does not respond to the command; so it just gives the default command text. So yeah, what i want to do:

If iTunes is not found, as given by the command, it should display

iTunes not found

If it is found and terminated,

iTunes is killed

Help? the errorlevel's don't work, this seem to be the fault of the nul not working.

Lunate answered 13/12, 2009 at 11:41 Comment(0)
T
11

Works for me at least:

> taskkill /f /im powershell.exe && echo worked || echo not worked
SUCCESS: The process "powershell.exe" with PID 3228 has been terminated.
worked

> taskkill /f /im powershell.exe && echo worked || echo not worked
ERROR: The process "powershell.exe" not found.
not worked

So taskkill is returning a proper exit code. The redirect of its output has nothing to do with this. But the error level for failure is 128. You really should use the proper idiom for checking for errors.

Also it seems that taskkill is printing to stderr so you see its output still, when just redirecting stdout. You may consider rewriting above code to:

taskkill /F /IM itunes.exe >nul 2>&1
if errorlevel 1 (echo iTunes not found.) else (echo iTunes is killed.)

The 2>&1 redirects the standard error output into the vast nothingness. if errorlevel 1 checks for errorlevel being at least 1 which should work at this point:

ERRORLEVEL number Specifies a true condition if the last program run returned an exit code equal to or greater than the number specified. —help if

Generally checking errorlevel with if %errorlevel%== is a quite bad idea, unless you're comparing to 0. The semantics for exit codes are that anything non-zero signals failure. Your assumption here just was that taskkill would return 1 on failure.

Ans may I kindly ask why you are doing this in an endless loop? taskkill already kills all instances of itunes.exe. And you're running in a tight loop without any delays so your batch files probably consumes one CPU core while it's running.

ETA: Overlooked your edit: Why on earth curly braces? Blocks in batch files are delimieted by round parentheses.

Tanh answered 13/12, 2009 at 11:50 Comment(4)
why on eart curly braces, well, i use them as long as i worked with them. I don't know; i just like it. It looks nicer i guess.Lunate
Well, if you're using them in batch files, then expect things going wrong. If you write code in a language you have to use the language's grammar, regardless what looks nicer or not. Curly braces don't work as block delimiters in batch.Tanh
i thought they did. At least they worked for the codes i used. I work with batch for over 1 year now, and have developed with curly braces, over and over, and it works fine. My customers (aka youtubers ;)) say it works fine for them.Lunate
oh, and about the endless loop: i edited the thing so that at else it says: else(goto :killed). In :killed, we have an additional 2 times taskkill, just to be sure, and after that, it echo a text, and tells the user it should press any key to exit.Lunate
N
0

an alternative solution, in vbscript, save below code as mykill.vbs

Set objArgs = WScript.Arguments
strProcess = objArgs(0)
strComputer = "."
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colProcesses = objWMIService.ExecQuery("Select * from Win32_Process Where Name ='" & strProcess & "'")

If colProcesses.Count = 0 Then
    Wscript.Echo strProcess & " is not running."
Else
    Wscript.Echo strProcess & " is running."
    'Kill the process
    For Each objProcess in colProcesses
        r=objProcess.Terminate()
        WScript.Echo "r is " & r
        If r = 0 Then
            WScript.Echo strProcess & " killed"
        End If
    Next        
End If

on the command line

c:\test> cscript //nologo mykill.vbs "itunes.exe"
Neogene answered 13/12, 2009 at 11:53 Comment(0)
N
0

Why don't you use PowerShell?

try
{
    Stop-Process -Name itunes -ErrorAction Stop
    "iTunes is killed"
}
catch
{
    "iTunes not found"
}
Noninterference answered 12/12, 2016 at 16:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.