Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

If Notepad.exe is running then taskkill if not running go to next statement

I need help writing batch code.

In the initial state of my batch script I need to check if notepad.exe is running if it is running then
taskkill /im notepad.exe elsif notepad.exe is not running then go to next batch statement/code.

like image 668
Mowgli Avatar asked Apr 09 '13 13:04

Mowgli


People also ask

How do you Taskkill in notepad?

For Example - To kill Notepad, run the command as, taskkill /IM notepad.exe /F, where /F is used to kill the process forcefully.

How do you check whether a process is running or not in Windows?

We can easily check whether a process is currently running or not using the tasklist toolkit. Tasklist allows us to check for current processes.

How can I see what processes are running in CMD?

You can use the ps command to find out which processes are running and display information about those processes. The ps command has several flags that enable you to specify which processes to list and what information to display about each process.

How do I kill a batch file?

taskkill /F /IM notepad.exe this is the best way to kill the task from task manager. Save this answer. Show activity on this post.


2 Answers

Try taskkill /fi "IMAGENAME eq notepad.exe" Not finding notepad.exe will only throw an info instead of an error.

like image 195
tomdemuyt Avatar answered Nov 08 '22 21:11

tomdemuyt


You can simply execute taskkill /im notepad.exe in all cases. If it's not running, then taskill will having nothing to kill and will just return.

In that situation, taskkill will report an error and set the error level. You can suppress the reporting of the error by redirecting standard error:

taskkill /im notepad.exe 2> nul

As for the error level, you can just ignore that and it will be cleared by the next command that you execute. Or if needed, you can clear it yourself.

This approach is, in my view, better than trying to anticipate whether or not taskkill will succeed. You won't be able to anticipate all possible failure modes and since taskkill itself performs the very check that you are asking about, I think you may as well leave that check to taskkill.

like image 34
David Heffernan Avatar answered Nov 08 '22 21:11

David Heffernan