Jump to content

kill child process


ur
 Share

Recommended Posts

I have created below code to run the python file.

#RequireAdmin
#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_Icon=icon.ico
#AutoIt3Wrapper_Outfile=RunTaskRun.Exe
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
#cs ----------------------------------------------------------------------------

 AutoIt Version: 3.3.14.0
 Author:        Uday Kiran Reddy(ur)

 Script Function:
    To log python data to a file.

#ce ----------------------------------------------------------------------------
#include <MsgBoxConstants.au3>
#include "C:\Automation\ISMBuild\Library.au3"
#include "ProcessEx_AddedNewEntryForLogging.au3"
#include "CheckChangeinCommit.au3"

If not NoChangesRequired() Then
    SendMail("Changes are in commit of erwin-main Repo","Will intimate once binaries are copied to Installshield machine")
    $hProcessHandle = _Process_RunCommand($PROCESS_RUN, $PROCESS_COMMAND & "C:\Python27\python.exe C:\BuildServer\AutoBuildServer\TaskRun.py") ; Capture the Process Handle
    $iPID = @extended ; Note the PID
    $returncode = _Process_DebugLogRunCommand($hProcessHandle, $iPID) ; Display the results in real-time
    Logging("Completed with ReturnCode "&$returncode)
Else
    SendMail("No Changes are not there in commit of erwin-main Repo","So no Binaries for today.If it is needed, please remove the text file from location: "& @TempDir&"\git_erwin_commit.txt")
EndIf

When I kill the autoit execution exe in the middle of execution, it is not terminating the python.exe launched from script.

Can you suggest how to do this?

Link to comment
Share on other sites

  • Developers

Define Kill process?

When truly killing the process, one can't expect any cleanup, but when you "instruct" the main process to terminate itself, you could also ensure all cleanup is arranged before terminating itself.

Jos

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

On 2/4/2017 at 1:34 PM, Subz said:

Have a look at OnAutoItExitRegister function in the help file.

Through this I can wireup a function to execute at the end.

But I can't kill python.exe directly in this function as it might kill other scripts running also instead of the one I invoke.

Anyother approach so that I can kill only the one I launched through my program if I click on the exit at the tray icon of the main script?

Link to comment
Share on other sites

After rereading the ProcessEx UDF I realized that it uses CMD to capture the process exit codes etc..  So found an alternative by getting the before and after snapshots of python.exe in the ProcessList this should return the new python.exe as $hPID which allows you to close the process.

#RequireAdmin
#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_Icon=icon.ico
#AutoIt3Wrapper_Outfile=RunTaskRun.Exe
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
#cs ----------------------------------------------------------------------------

 AutoIt Version: 3.3.14.0
 Author:        Uday Kiran Reddy(ur)

 Script Function:
    To log python data to a file.

#ce ----------------------------------------------------------------------------
#include <MsgBoxConstants.au3>
#include "C:\Automation\ISMBuild\Library.au3"
#include "ProcessEx_AddedNewEntryForLogging.au3"
#include "CheckChangeinCommit.au3"

If not NoChangesRequired() Then
    SendMail("Changes are in commit of erwin-main Repo","Will intimate once binaries are copied to Installshield machine")
    Local $aProcessList_Before = ProcessList('python.exe')
    $hProcessHandle = _Process_RunCommand($PROCESS_RUN, $PROCESS_COMMAND & "C:\Python27\python.exe C:\BuildServer\AutoBuildServer\TaskRun.py") ; Capture the Process Handle
    Do
        $aProcessList_After = ProcessList('python.exe')
    Until $aProcessList_After[0][0] > $aProcessList_Before[0][0]
    $hPID = Number($aProcessList_After[$aProcessList_After[0][0]][1])
    $returncode = _Process_DebugLogRunCommand($hProcessHandle, $iPID) ; Display the results in real-time
    Logging("Completed with ReturnCode "&$returncode)
Else
    SendMail("No Changes are not there in commit of erwin-main Repo","So no Binaries for today.If it is needed, please remove the text file from location: "& @TempDir&"\git_erwin_commit.txt")
EndIf

 

Edited by Subz
Modified code
Link to comment
Share on other sites

19 hours ago, Subz said:

After rereading the ProcessEx UDF I realized that it uses CMD to capture the process exit codes etc..  So found an alternative by getting the before and after snapshots of python.exe in the ProcessList this should return the new python.exe as $hPID which allows you to close the process.

#RequireAdmin
#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_Icon=icon.ico
#AutoIt3Wrapper_Outfile=RunTaskRun.Exe
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
#cs ----------------------------------------------------------------------------

 AutoIt Version: 3.3.14.0
 Author:        Uday Kiran Reddy(ur)

 Script Function:
    To log python data to a file.

#ce ----------------------------------------------------------------------------
#include <MsgBoxConstants.au3>
#include "C:\Automation\ISMBuild\Library.au3"
#include "ProcessEx_AddedNewEntryForLogging.au3"
#include "CheckChangeinCommit.au3"

If not NoChangesRequired() Then
    SendMail("Changes are in commit of erwin-main Repo","Will intimate once binaries are copied to Installshield machine")
    Local $aProcessList_Before = ProcessList('python.exe')
    $hProcessHandle = _Process_RunCommand($PROCESS_RUN, $PROCESS_COMMAND & "C:\Python27\python.exe C:\BuildServer\AutoBuildServer\TaskRun.py") ; Capture the Process Handle
    Do
        $aProcessList_After = ProcessList('python.exe')
    Until $aProcessList_After[0][0] > $aProcessList_Before[0][0]
    $hPID = Number($aProcessList_After[$aProcessList_After[0][0]][1])
    $returncode = _Process_DebugLogRunCommand($hProcessHandle, $iPID) ; Display the results in real-time
    Logging("Completed with ReturnCode "&$returncode)
Else
    SendMail("No Changes are not there in commit of erwin-main Repo","So no Binaries for today.If it is needed, please remove the text file from location: "& @TempDir&"\git_erwin_commit.txt")
EndIf

THank you very much.

I will try this code and let you know my inputs.

 

Link to comment
Share on other sites

On 2/4/2017 at 3:17 AM, Jos said:

Define Kill process?

When truly killing the process, one can't expect any cleanup, but when you "instruct" the main process to terminate itself, you could also ensure all cleanup is arranged before terminating itself.

Jos

I have to ask now, is there a command to "instruct" the process to terminate itself? I've never noticed it but that would be handy indeed.

 

 

Link to comment
Share on other sites

  • Developers
13 hours ago, BetaLeaf said:

is there a command to "instruct" the process to terminate itself?

When your script has an GUI, it is easy to send a WinClose message, which then can be handled by the script as the GUI gets closed.
Another option is to monitor for a specific message and act on that.

Jos 

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
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
 Share

×
×
  • Create New...