Is there a command to close a running program
-
Huidong
- Posts: 213
- Joined: 18 May 2011 21:55
Is there a command to close a running program
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
Yes, via run or cmd.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!
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
TSKILL is part of Windows 7. Alternatively, psexec as part of the Sysinternals Suite can be used.serendipity wrote:Yes, via run or cmd.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!
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.
-
Huidong
- Posts: 213
- Joined: 18 May 2011 21:55
Re: Is there a command to close a running program
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
You can omit .exe and use defraggler. You get the process names from task manager's process.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.
::run "cmd /c TSKILL defraggler";
-
Huidong
- Posts: 213
- Joined: 18 May 2011 21:55
Re: Is there a command to close a running program
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
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
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.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?
Code: Select all
pslist defraggler.exe
if errorlevel 0 ( //Running.
tskill defraggler.exe
) else ( //Implies errorlevel 1 i.e. not running.
//Alternative operation.
) -
Huidong
- Posts: 213
- Joined: 18 May 2011 21:55
Re: Is there a command to close a running program
I tried the command above but defraggler is not killed! Why?serendipity wrote:You can omit .exe and use defraggler. You get the process names from task manager's process.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.
::run "cmd /c TSKILL defraggler";
-
Huidong
- Posts: 213
- Joined: 18 May 2011 21:55
Re: Is there a command to close a running program
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
I thought "Ho that's so cool!" killed defraggler before.Huidong wrote:I tried the command above but defraggler is not killed! Why?serendipity wrote:You can omit .exe and use defraggler. You get the process names from task manager's process.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.
::run "cmd /c TSKILL defraggler";
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
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
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
then ::run "cmd /c TSKILL defraggler"; should work.Huidong wrote:What's "image name"? The process name is just Defraggler.exe, is that what you're asking?
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
TSKILL defraggler in cmd worked!
But again,
But again,
does not work!run "cmd /c TSKILL defraggler"
XYplorer Beta Club