Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

Check if a Process Exists. If it does, kill it.



  • Please log in to reply
18 replies to this topic
Spektre
  • Members
  • 6 posts
  • Last active: Dec 12 2014 10:54 PM
  • Joined: 12 Dec 2014

I have a script that needs to look if a process is running.

 

The candidate process' root name is located in a variable names "Table"

 

The full name of the process would be %Table%.exe

 

I'm having difficulty with the syntax for checking of this process exists and if it does closing it.

 

(Also the process has a *32 following it in the Task Manager.  Does this *32 need appended to the name?)

 

Thanks in advance.



Masonjar13
  • Members
  • 1517 posts
  • Last active:
  • Joined: 16 Sep 2012

Process, Exist, process.exe
if(!errorlevel)
    Process, Close, process.exe

OS: Windows 7 Ultimate / Windows 8.1 Pro | Editor: Notepad++


Guest10
  • Members
  • 1216 posts
  • Last active: Oct 30 2015 05:12 PM
  • Joined: 27 Oct 2012

In this example, how often the process is poled (checked) for ErrorLevel?



girlgamer
  • Moderators
  • 3263 posts
  • Last active: Feb 01 2015 09:49 AM
  • Joined: 04 Jun 2010

Unless it's looped, the routine shown will execute one time.


The universe is a wondrous place! The faster you create unbreakable code, the faster the universe creates people that can break it. All scripting follows the rule Rule Of Twos -- 1) Good, 2) Fast 3) Cheap -- pick any Two.
I guarantee absolutely nothing about any code I provide except that it works in my machine. ●
MMO Fighter   KeyLooperDemo   Key Spammer   TinyClickRecorder  GGs Password Generator.ahk
For the newest version of AutoHotkey and some killer scripts go here.
Rock-on%20kitten.gif


Spektre
  • Members
  • 6 posts
  • Last active: Dec 12 2014 10:54 PM
  • Joined: 12 Dec 2014
Process, Exist, process.exe
if(!errorlevel)
    Process, Close, process.exe

 

I've tried a variation on this.  This snippet isn;t exactly what was asked for as the process name (minus the .exe) is held in a variable.  I suspect it is my use of variables that is wrong.

 

Process, Exist, %Table%.exe

If(!errorlevel)

{

Process, Close, %Table%.exe

}

 

Does not work.

 

 

In this example, how often the process is poled (checked) for ErrorLevel?

 

It is checked hundreds of times.  The process name (absent the .exe) is stored in the variable Table.  This is looped over from a list of file names in a directory.



Exaskryz
  • Members
  • 3249 posts
  • Last active: Nov 20 2015 05:30 AM
  • Joined: 23 Aug 2012

You can try %Table% . .exe or % Table . ".exe" for where the name should go, or you can create a new variable that appends the .exe onto the value. (I haven't tried any of that)



Oldman
  • Members
  • 2475 posts
  • Last active: Feb 18 2015 04:57 PM
  • Joined: 01 Dec 2013

I've tried a variation on this.  This snippet isn;t exactly what was asked for as the process name (minus the .exe) is held in a variable.  I suspect it is my use of variables that is wrong.

 

Process, Exist, %Table%.exe

If(!errorlevel)

{

Process, Close, %Table%.exe

}

 

Does not work.

 

 

 

It is checked hundreds of times.  The process name (absent the .exe) is stored in the variable Table.  This is looped over from a list of file names in a directory.

 

What is the problem ?

The script can't find the process or the script can't close the process ?

Because, the syntax is valid, assuming that the variable contains the process name without its extension.


Si ton labeur est dur et que tes résultats sont minces, souviens toi du grand chêne qui avant n'était qu'un gland....comme toi ! (anonyme) ;)

L'art de lire, c'est l'art de penser avec un peu d'aide. (É. Faguet)

Windows 3.1. Collector's Edition.     (www.avaaz.org)


TravisQ
  • Members
  • 86 posts
  • Last active: Jun 26 2015 05:39 PM
  • Joined: 25 Oct 2014
Table=Notepad
Process, Exist, %Table%.exe
WinClose,ahk_pid %ErrorLevel%


GEV
  • Members
  • 1364 posts
  • Last active:
  • Joined: 23 Oct 2013

What is the problem ?

The script can't find the process or the script can't close the process ?

 

To find this out you can use Example2 in http://ahkscript.org...ss.htm#Examples

 

 

Table = Notepad

Process, wait, %Table%.exe, 2
NewPID = %ErrorLevel%  ; Save the value immediately since ErrorLevel is often changed.
if NewPID = 0
{
    MsgBox The specified process did not appear within 2 seconds.
    return
}
; Otherwise:
MsgBox A matching process has appeared (Process ID is %NewPID%).
Process, priority, %NewPID%, Low
Process, priority, , High  ; Have the script set itself to high priority.

; WinClose Untitled - Notepad

Process, Close, %NewPID%

Process, WaitClose, %NewPID%, 5
if ErrorLevel ; The PID still exists.
    MsgBox The process did not close within 5 seconds.



Exaskryz
  • Members
  • 3249 posts
  • Last active: Nov 20 2015 05:30 AM
  • Joined: 23 Aug 2012

GEV has a typo right here, in his bolded line: Process, Close, %NeuPID% which should of course be Process, Close, %NewPID%



GEV
  • Members
  • 1364 posts
  • Last active:
  • Joined: 23 Oct 2013

GEV has a typo right here, in his bolded line: Process, Close, %NeuPID% which should of course be Process, Close, %NewPID%

Thanks for the correction Exaskryz. But I found out that it doesn't work and erased it.

 

EDIT: Sorry It works!!



Oldman
  • Members
  • 2475 posts
  • Last active: Feb 18 2015 04:57 PM
  • Joined: 01 Dec 2013

Thanks for these enthusiastic supports, but there is a misunderstanding.
My question was specifically addressed to Spektre.
I wanted to know what problem he faced.


Si ton labeur est dur et que tes résultats sont minces, souviens toi du grand chêne qui avant n'était qu'un gland....comme toi ! (anonyme) ;)

L'art de lire, c'est l'art de penser avec un peu d'aide. (É. Faguet)

Windows 3.1. Collector's Edition.     (www.avaaz.org)


Spektre
  • Members
  • 6 posts
  • Last active: Dec 12 2014 10:54 PM
  • Joined: 12 Dec 2014

 

Table=Notepad
Process, Exist, %Table%.exe
WinClose,ahk_pid %ErrorLevel%

 

This code will not execute.  %Table%.exe is not a valid parameter.

 

OK, I tried the following as a test case.

 

I have a process running named 4_Million.exe

 

Table = 4_Million

Process Exist, %Table%.exe

 

The script does not find the process running.  It is.



Spektre
  • Members
  • 6 posts
  • Last active: Dec 12 2014 10:54 PM
  • Joined: 12 Dec 2014

My original code was as such: 

If ProcessExists(%Table%.exe)
{
Process, Close, %Table%.exe
}

ProcessExists(Name){
	Process,Exist,%Name%
	return Errorlevel
}

This never seemed to execute so I tried the simpler versions shown here.



TravisQ
  • Members
  • 86 posts
  • Last active: Jun 26 2015 05:39 PM
  • Joined: 25 Oct 2014
Table=Notepad
loop {
ProcessExists(Table)
}
ProcessExists(Name){
	Process,Exist,%Name%.exe
	Process,close,%errorlevel%
}