How to work with external child processes

The process builder (GtExternalProcessBuilder Object subclass: #GtExternalProcessBuilder instanceVariableNames: 'command arguments environmentVariablesCommands stdout stderr stdin workingDirectory windowsProcessCreationFlags' classVariableNames: 'WINDOWS_CREATE_NO_WINDOW WINDOWS_DETACHED_PROCESS' package: 'GToolkit-ExternalProcess-Core' ) allows developers to create and spawn external child processes. It provides configuration api to pass arguments to the command or redirect standard streams (stdout, stderr and stdin.

Asynchronous output

The following snippet creates an indefinitely running process that regularly pings the localhost and configures the standard streams to be piped into a buffer:

process := GtExternalProcessBuilder new
	command: 'ping';
	arg: '';

The piped stdout is buffered and can be polled at regular intervals. The code snippet below polls the stdout as a string every 0.5 seconds and prints it into a local transcript:

transcript := GtTranscript new.

reader := process asynchronousStdout
	pollStringEvery: 0.5 second
	do: [ :aString | transcript show: aString ].


At wish, the running process can be killed, stopping the asynchronous polling of the output:

process kill

Synchronous output

If desired, a command can be executed synchronously blocking the current running process:

output := GtExternalProcessBuilder new
	command: 'date';
output stdout