Jump to content

Recommended Posts

Posted (edited)

Hello!

I need to run cmd line program and if it is running longer than defined time, kill it.

I have it done by this script.

But I dont know how to add next feature:  If the program ends in-time, then read its Exit Code (It should be number from 0 to 255).

PLS can anyone help?

If FileChangeDir("C:\X") <> 1 Then Exit
$PID = Run('"C:\Program Files\WinRAR\Rar.exe" A -TS -T "C:\X\Archive.RAR" *', "", @SW_HIDE)
If @Error <> 0 Then Exit
$C = TimerInit()
While 1
   Sleep(250)
   If ProcessExists($PID) = 0 Then ExitLoop
   If TimerDiff($C) > 10000 Then
      RunWait("TaskKill /F /PID " & $PID, "", @SW_HIDE)
   EndIf
WEnd
$ExitCode = ""   ; ???? HOW-TO?

 

Edited by iXX
Solved.
Posted

I know, for return exit code of program normally works RunWait, but in this case, how to program the timeout?

If the program hangs or something, RunWait will wait forever...:(

Posted

Try here, might need some tweaking to work with newer versions.

 

 

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Posted

you should redirect the output stream of rar.exe to your script, so to catch the output,
2 changes in you script are needed:
1) setup the redirection stream to your script within the "run()" command
2) read the output stream to get rar.exe output...
following script should do what you need

#include <AutoItConstants.au3>
If FileChangeDir("C:\X") <> 1 Then Exit
$PID = Run('"C:\Program Files\WinRAR\Rar.exe" A -TS -T "C:\X\Archive.RAR" *', "", @SW_HIDE, $STDERR_MERGED)
If @error <> 0 Then Exit
$C = TimerInit()
While 1
    Sleep(250)
    If ProcessExists($PID) = 0 Then ExitLoop
    If TimerDiff($C) > 10000 Then
        RunWait("TaskKill /F /PID " & $PID, "", @SW_HIDE)
    EndIf
WEnd
$ExitCode = StdoutRead($PID) ; ???? HOW-TO?

ConsoleWrite($ExitCode & @CRLF)

 

 

image.jpeg.9f1a974c98e9f77d824b358729b089b0.jpeg Chimp

small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

Posted

Just tryed, but while RunWait returns just needed ExitCode number, the edited script above pukes text of rar's progress....:'(

 

Posted (edited)

maybe something like this...

; example from this post by @funkey:
; https://www.autoitscript.com/forum/topic/152133-run-runwait-and-exit-codes/?do=findComment&comment=1091035

#include <ProcessConstants.au3>
#include <WinAPIProc.au3>
#include <WinAPISys.au3>

Local $sCmd = '"C:\Program Files\WinRAR\Rar.exe" A -TS -T "C:\X\Archive.RAR" *'

If FileChangeDir("C:\X") <> 1 Then Exit

AdlibRegister("Timeout", 10000) ; max allowed 10 seconds

Global $iPID = Run($sCmd, "", @SW_HIDE)
If Not $iPID Then Exit

Global $hProcess
If _WinAPI_GetVersion() >= 6.0 Then
    $hProcess = _WinAPI_OpenProcess($PROCESS_QUERY_LIMITED_INFORMATION, 0, $iPID)
Else
    $hProcess = _WinAPI_OpenProcess($PROCESS_QUERY_INFORMATION, 0, $iPID)
EndIf
If Not $hProcess Then Exit
ProcessWaitClose($iPID)
Global $ExitCode = DllCall("kernel32.dll", "bool", "GetExitCodeProcess", "HANDLE", $hProcess, "dword*", -1)
_WinAPI_CloseHandle($hProcess)
ConsoleWrite("Exit code: " & $ExitCode[2] & @LF)

Func Timeout()
    RunWait("TaskKill /F /PID " & $iPID, "", @SW_HIDE)
EndFunc   ;==>Timeout

 

Edited by Chimp
added _WinAPI_CloseHandle in code

 

image.jpeg.9f1a974c98e9f77d824b358729b089b0.jpeg Chimp

small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

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
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...