Is there a command to close a running program

Discuss and share scripts and script files...
Huidong
Posts: 213
Joined: 18 May 2011 21:55

Is there a command to close a running program

Post by Huidong »

I don't see such a command myself, so I wonder if it's possible to terminate a process on demand by scripting? Thanks!

serendipity
Posts: 3360
Joined: 07 May 2007 18:14
Location: NJ/NY

Re: Is there a command to close a running program

Post by serendipity »

Huidong wrote:I don't see such a command myself, so I wonder if it's possible to terminate a process on demand by scripting? Thanks!
Yes, via run or cmd.
Example:
Open notepad and run this:
::run "cmd /c TSKILL notepad";
notepad should close.
This is for XP though, not sure if this works on win7 or not.

nas8e9
Posts: 2232
Joined: 21 Jun 2008 14:50

Re: Is there a command to close a running program

Post by nas8e9 »

serendipity wrote:
Huidong wrote:I don't see such a command myself, so I wonder if it's possible to terminate a process on demand by scripting? Thanks!
Yes, via run or cmd.
Example:
Open notepad and run this:
::run "cmd /c TSKILL notepad";
notepad should close.
This is for XP though, not sure if this works on win7 or not.
TSKILL is part of Windows 7. Alternatively, psexec as part of the Sysinternals Suite can be used.

Huidong
Posts: 213
Joined: 18 May 2011 21:55

Re: Is there a command to close a running program

Post by Huidong »

It works for notepad on Win7, thanks! But I don't know how to do it in general. Say I want to close D:\Portable\Defraggler\Defraggler.exe, how to put quotes properly? I tried but none worked.

serendipity
Posts: 3360
Joined: 07 May 2007 18:14
Location: NJ/NY

Re: Is there a command to close a running program

Post by serendipity »

Huidong wrote:It works for notepad on Win7, thanks! But I don't know how to do it in general. Say I want to close D:\Portable\Defraggler\Defraggler.exe, how to put quotes properly? I tried but none worked.
You can omit .exe and use defraggler. You get the process names from task manager's process.

::run "cmd /c TSKILL defraggler";

Huidong
Posts: 213
Joined: 18 May 2011 21:55

Re: Is there a command to close a running program

Post by Huidong »

Ho that's so cool! Thank you!

Huidong
Posts: 213
Joined: 18 May 2011 21:55

Re: Is there a command to close a running program

Post by Huidong »

Oh, one more question, how to use if...else... to close the program only if it's running? i.e. how to check if a program is running? Reason: I noticed that the command-line exe of defraggler (df.exe) does not work if the GUI exe is running, so after I wrote a command to defrag selected items, I'd like to add that check beforehand so that the GUI gets killed before attempting to run df.exe. Could you help?

nas8e9
Posts: 2232
Joined: 21 Jun 2008 14:50

Re: Is there a command to close a running program

Post by nas8e9 »

Huidong wrote:Oh, one more question, how to use if...else... to close the program only if it's running? i.e. how to check if a program is running? Reason: I noticed that the command-line exe of defraggler (df.exe) does not work if the GUI exe is running, so after I wrote a command to defrag selected items, I'd like to add that check beforehand so that the GUI gets killed before attempting to run df.exe. Could you help?
Sysinternals PSList can be used for this. It will return a errorlevel of 0 if the process is running and of 1 if it's not.

Code: Select all

pslist defraggler.exe
   if errorlevel 0 (                  //Running.
      tskill defraggler.exe
   ) else (                           //Implies errorlevel 1 i.e. not running.
      //Alternative operation.
   ) 
Based on your description I wonder if it's necessary to actually test for a process running first before trying to terminate it; the TSKILL command will simply return an error and allow the script/batch file to continue?

Huidong
Posts: 213
Joined: 18 May 2011 21:55

Re: Is there a command to close a running program

Post by Huidong »

serendipity wrote:
Huidong wrote:It works for notepad on Win7, thanks! But I don't know how to do it in general. Say I want to close D:\Portable\Defraggler\Defraggler.exe, how to put quotes properly? I tried but none worked.
You can omit .exe and use defraggler. You get the process names from task manager's process.

::run "cmd /c TSKILL defraggler";
I tried the command above but defraggler is not killed! Why?

Huidong
Posts: 213
Joined: 18 May 2011 21:55

Re: Is there a command to close a running program

Post by Huidong »

OMG, even run "cmd /c TSKILL notepad"; stops working!! Is my computer wrong or sth? I believe that I just tried that script and it worked once!!! Ghost!

serendipity
Posts: 3360
Joined: 07 May 2007 18:14
Location: NJ/NY

Re: Is there a command to close a running program

Post by serendipity »

Huidong wrote:
serendipity wrote:
Huidong wrote:It works for notepad on Win7, thanks! But I don't know how to do it in general. Say I want to close D:\Portable\Defraggler\Defraggler.exe, how to put quotes properly? I tried but none worked.
You can omit .exe and use defraggler. You get the process names from task manager's process.

::run "cmd /c TSKILL defraggler";
I tried the command above but defraggler is not killed! Why?
I thought "Ho that's so cool!" killed defraggler before.
Can you open task manager and see what the image name is underprocess? I just renamed defraggler.exe to killmeplz.exe and killed it using this:

::run "cmd /c TSKILL killmeplz";

Huidong
Posts: 213
Joined: 18 May 2011 21:55

Re: Is there a command to close a running program

Post by Huidong »

Now I can't even kill notepad! It feels like I'm in a horror move! Any idea what happened to my computer? I just killed notepad once, I think.

Huidong
Posts: 213
Joined: 18 May 2011 21:55

Re: Is there a command to close a running program

Post by Huidong »

What's "image name"? The process name is just Defraggler.exe, is that what you're asking?

serendipity
Posts: 3360
Joined: 07 May 2007 18:14
Location: NJ/NY

Re: Is there a command to close a running program

Post by serendipity »

Huidong wrote:What's "image name"? The process name is just Defraggler.exe, is that what you're asking?
then ::run "cmd /c TSKILL defraggler"; should work.

You can always open command prompt and test it from there first.

First type "tasklist" to see if you see the process "defraggler.exe" there.

if so use:
TSKILL defraggler

does it work?

Huidong
Posts: 213
Joined: 18 May 2011 21:55

Re: Is there a command to close a running program

Post by Huidong »

TSKILL defraggler in cmd worked!
But again,
run "cmd /c TSKILL defraggler"
does not work!

Post Reply