Sign in to follow this  
Followers 0
Vaner

Kill (x) process after running for (x) time

3 posts in this topic

i was needed to kill some job running on a pc , after a period of time .

ex. all notepad that been running for 12h , must be close .

will not affect newer process .

u can change $iDateCalc > $timeup to $iDateCalc < $timeup will kill (x) process a soon it start

#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_outfile=Ghostsvrcleanup.exe
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
#cs ----------------------------------------------------------------------------

 AutoIt Version: 3.2.5.3 (beta)
 Author:         myName

 Script Function:
    Process monitor

#ce ----------------------------------------------------------------------------

#include <Date.au3>

Func ProcessMon()


$process = "'ghostsrv.exe'"
$timeup = "12"
$wbemFlagReturnImmediately = 0x10
$wbemFlagForwardOnly = 0x20
$colItems = ""
$strComputer = "localhost"  
$objWMIService = ObjGet("winmgmts:\\" & $strComputer & "\root\CIMV2")
$colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_Process Where NAME = " & $process &"", "WQL", $wbemFlagReturnImmediately + $wbemFlagForwardOnly)


If IsObj($colItems) then
   For $objItem In $colItems
      $time = WMIDateStringToDate($objItem.CreationDate)
      $PID = $objItem.ProcessId 
      $Starttime = StringRight($time, 8)
      ;Msgbox(1,"WMI Outputime",$time)
      $Date = StringTrimRight($time, 9)
      ;Msgbox(1,"WMI Outputdate",$pid)
      $array = StringSplit($date, '/', 1)
      $FixDate = $array[3] & '/' & $array[1] &'/' & $array[2] & ' ' & $Starttime 
       ;Msgbox(1,"WMI fixdate",$FixDate)
      $iDateCalc = _DateDiff( 'h',$FixDate,_NowCalc())
      ;Msgbox(1,"nowcalc",$iDateCalc)
      if $iDateCalc > $timeup then
         ProcessClose($PID) 
      EndIf

       Next 
Else
   Msgbox(0,"WMI Output","No WMI Objects Found for class: " & "Win32_Process" )
Endif
  $time =""
  $PID=""

EndFunc
 
Func WMIDateStringToDate($dtmDate)

    Return (StringMid($dtmDate, 5, 2) & "/" & _
    StringMid($dtmDate, 7, 2) & "/" & StringLeft($dtmDate, 4) _
    & " " & StringMid($dtmDate, 9, 2) & ":" & StringMid($dtmDate, 11, 2) & ":" & StringMid($dtmDate,13, 2))
EndFunc

ProcessMon()

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

I simply love it, i for one can find good use for this, thanks <_<

Edited by jokke

UDF:Crypter a file encrypt / decrypt tool with no need to remember a password again. Based on Caesar cipher using entire ASCII Table.Script's: PixelSearch Helper, quick and simple way to create a PixelSeach.Chatserver - simplified, not so complicated multi-socket server.AutoIT - Firewall, simple example on howto create a firewall with AutoIt.

Share this post


Link to post
Share on other sites

i was needed to kill some job running on a pc , after a period of time .

ex. all notepad that been running for 12h , must be close .

will not affect newer process .

u can change $iDateCalc > $timeup to $iDateCalc < $timeup will kill (x) process a soon it start

#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_outfile=Ghostsvrcleanup.exe
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
#cs ----------------------------------------------------------------------------

 AutoIt Version: 3.2.5.3 (beta)
 Author:         myName

 Script Function:
    Process monitor

#ce ----------------------------------------------------------------------------

#include <Date.au3>

Func ProcessMon()


$process = "'ghostsrv.exe'"
$timeup = "12"
$wbemFlagReturnImmediately = 0x10
$wbemFlagForwardOnly = 0x20
$colItems = ""
$strComputer = "localhost"  
$objWMIService = ObjGet("winmgmts:\\" & $strComputer & "\root\CIMV2")
$colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_Process Where NAME = " & $process &"", "WQL", $wbemFlagReturnImmediately + $wbemFlagForwardOnly)


If IsObj($colItems) then
   For $objItem In $colItems
      $time = WMIDateStringToDate($objItem.CreationDate)
      $PID = $objItem.ProcessId 
      $Starttime = StringRight($time, 8)
      ;Msgbox(1,"WMI Outputime",$time)
      $Date = StringTrimRight($time, 9)
      ;Msgbox(1,"WMI Outputdate",$pid)
      $array = StringSplit($date, '/', 1)
      $FixDate = $array[3] & '/' & $array[1] &'/' & $array[2] & ' ' & $Starttime 
       ;Msgbox(1,"WMI fixdate",$FixDate)
      $iDateCalc = _DateDiff( 'h',$FixDate,_NowCalc())
      ;Msgbox(1,"nowcalc",$iDateCalc)
      if $iDateCalc > $timeup then
         ProcessClose($PID) 
      EndIf

       Next 
Else
   Msgbox(0,"WMI Output","No WMI Objects Found for class: " & "Win32_Process" )
Endif
  $time =""
  $PID=""

EndFunc
 
Func WMIDateStringToDate($dtmDate)

    Return (StringMid($dtmDate, 5, 2) & "/" & _
    StringMid($dtmDate, 7, 2) & "/" & StringLeft($dtmDate, 4) _
    & " " & StringMid($dtmDate, 9, 2) & ":" & StringMid($dtmDate, 11, 2) & ":" & StringMid($dtmDate,13, 2))
EndFunc

ProcessMon()

Is it possible to be modified script as to monitor if Process get 100% from CPU for $time?

In other words - to kill overloading process under Win2k3 ?

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0