Check if process is exist Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
Tomer
Posts: 366
Joined: 21 Aug 2016, 05:11

Check if process is exist

25 Aug 2016, 06:28

Hi,

I am new to AHK and would like to write a script that check if process cmd.exe is exist.
if not exist, then popup a msgbox: "cmd not existed!"
if exist, then kill\close cmd.exe process and popup a msgbox: "cmd killed!"

what am I doing wrong ?

Code: Select all

IfProcessExist, cmd.exe
{
     msgbox, cmd not existed!
     exit
}
else
    Process,Close,cmd.exe
    msgbox, cmd killed!
    
return
Thanks!
GEV
Posts: 1002
Joined: 25 Feb 2014, 00:50

Re: Check if process is exist

25 Aug 2016, 06:53

Code: Select all

F1::
IfWinNotExist, ahk_exe cmd.exe
{
     msgbox, cmd doesn't exist!
		return  ; or: exit
}
; otherwise:
ControlSend,, exit{Enter}, ahk_exe cmd.exe
Sleep, 500
IfWinNotExist, ahk_exe cmd.exe
	msgbox, cmd closed!
return
User avatar
Tomer
Posts: 366
Joined: 21 Aug 2016, 05:11

Re: Check if process is exist

25 Aug 2016, 07:12

Thanks GEV,

but If I have more then 1 cmd's processes running, it closes only the last cmd process that opened,
is it possible to terminate all of them ? also, to kill them all even if they're hidden.

Thanks!
GEV
Posts: 1002
Joined: 25 Feb 2014, 00:50

Re: Check if process is exist  Topic is solved

25 Aug 2016, 07:24

Code: Select all

F1::
DetectHiddenWindows, On
IfWinNotExist, ahk_exe cmd.exe
{
     DetectHiddenWindows, Off
     msgbox, cmd doesn't exist!
		return  ; or: exit
}
; otherwise:
WinGet, id, list, ahk_exe cmd.exe
Loop, %id%
{
	this_ID := id%A_Index%
	ControlSend,, exit{Enter}, ahk_id %this_ID%
	Sleep, 300
}
Sleep, 500
IfWinNotExist, ahk_exe cmd.exe
	msgbox, cmd closed!
DetectHiddenWindows, Off
return
Last edited by GEV on 25 Aug 2016, 07:54, edited 1 time in total.
User avatar
Tomer
Posts: 366
Joined: 21 Aug 2016, 05:11

Re: Check if process is exist

25 Aug 2016, 07:41

Perfect! thanks GEV!
User avatar
Tomer
Posts: 366
Joined: 21 Aug 2016, 05:11

Re: Check if process is exist

25 Aug 2016, 08:41

Sorry,
Update:

its works only when its empty cmd window,
when I try to use it while the cmd is running batch file,
the "exit" command doesn't help :(

I guess I cant use "ControlSend" in this situation and need other script.

tnx!
GEV
Posts: 1002
Joined: 25 Feb 2014, 00:50

Re: Check if process is exist

25 Aug 2016, 09:12

You can use this, but ONLY IF YOU ARE 100% SURE that NO DAMAGE can be caused to your system.

Code: Select all

F1::
DetectHiddenWindows, On
IfWinNotExist, ahk_exe cmd.exe
{
     DetectHiddenWindows, Off
	 msgbox, cmd doesn't exist!	 
		return  ; or: exit
}
; otherwise:
WinGet, id, list, ahk_exe cmd.exe
Loop, %id%
{
	this_ID := id%A_Index%
	ControlSend,, exit{Enter}, ahk_id %this_ID%
	Sleep, 300
}
Sleep, 500
IfWinExist, ahk_exe cmd.exe
{
	WinGet, id, list, ahk_exe cmd.exe
	Loop, %id%
	{
		this_ID := id%A_Index%
		WinGet, PID, PID, ahk_id %this_ID%
		Process, Close, %PID%
		Sleep, 300
	}
}
Sleep, 500
IfWinNotExist, ahk_exe cmd.exe
	msgbox, cmd closed!	
DetectHiddenWindows, Off
return
Tomer1

Re: Check if process is exist

25 Aug 2016, 10:14

Ok I finally got it :)
I use your code with some changes:

Code: Select all

DetectHiddenWindows, On
IfWinNotExist, ahk_exe cmd.exe
{
          msgbox, cmd doesn't exist!
		return  ; or: exit
}
; otherwise:
runwait, taskkill /f /im cmd.exe ,,hide\
sleep, 600
IfWinNotExist, ahk_exe cmd.exe
	msgbox, cmd closed!
DetectHiddenWindows, Off
return
thanks again !

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Exies, JoeWinograd, scriptor2016 and 98 guests