1.85 <GSubprocess>

Class: <GSubprocess>

It derives from <GObject>.

<GSubprocess> allows the creation of and interaction with child processes.

Processes can be communicated with using standard GIO-style APIs (ie: <GInputStream>, <GOutputStream>). There are GIO-style APIs to wait for process termination (ie: cancellable and with an asynchronous variant).

There is an API to force a process to terminate, as well as a race-free API for sending UNIX signals to a subprocess.

One major advantage that GIO brings over the core GLib library is comprehensive API for asynchronous I/O, such output-stream:splice-async. This makes <GSubprocess> significantly more powerful and flexible than equivalent APIs in some other languages such as the subprocess.py included with Python. For example, using <GSubprocess> one could create two child processes, reading standard output from the first, processing it, and writing to the input stream of the second, all without blocking the main loop.

A powerful subprocess:communicate API is provided similar to the communicate() method of subprocess.py. This enables very easy interaction with a subprocess that has been opened with pipes.

<GSubprocess> defaults to tight control over the file descriptors open in the child process, avoiding dangling-FD issues that are caused by a simple fork()/exec(). The only open file descriptors in the spawned process are ones that were explicitly specified by the <GSubprocess> API (unless inherit-fds was specified).

<GSubprocess> will quickly reap all child processes as they exit, avoiding ‘zombie processes’ remaining around for long periods of time. subprocess:wait? can be used to wait for this to happen, but it will happen even without the call being explicitly made.

As a matter of principle, <GSubprocess> has no API that accepts shell-style space-separated strings. It will, however, match the typical shell behaviour of searching the PATH for executables that do not contain a directory separator in their name. By default, the PATH of the current process is used. You can specify search-path-from-envp to use the PATH of the launcher environment instead.

<GSubprocess> attempts to have a very simple API for most uses (ie: spawning a subprocess with arguments and support for most typical kinds of input and output redirection). See subprocess:new. The <GSubprocessLauncher> API is provided for more complicated cases (advanced types of redirection, environment variable manipulation, change of working directory, child setup functions, etc).

A typical use of <GSubprocess> will involve calling subprocess:new, followed by subprocess:wait-async or subprocess:wait?. After the process exits, the status can be checked using functions such as subprocess:get-if-exited? (which are similar to the familiar WIFEXITED-style POSIX macros).

Note that as of GLib 2.82, creating a <GSubprocess> causes the signal SIGPIPE to be ignored for the remainder of the program. If you are writing a command-line utility that uses <GSubprocess>, you may need to take into account the fact that your program will not automatically be killed if it tries to write to stdout after it has been closed.

Properties

Property: subprocess:argv self

Argument vector.

Its value is a list or vector, each a string, or #f.

It can only be set when the object is made, with #:argv. It cannot be read.

Property: subprocess:flags self

Subprocess flags.

Its value is a <GSubprocessFlags>, or a list of its symbols, or one of them.

It can only be set when the object is made, with #:flags. It cannot be read.

Procedures

Method: subprocess:communicate self [stdin-buf] [cancellable] ⇒ %return stdout-buf stderr-buf
Method: communicate self [stdin-buf] [cancellable] ⇒ %return stdout-buf stderr-buf

Communicate with the subprocess until it terminates, and all input and output has been completed.

If stdin-buf is given, the subprocess must have been created with stdin-pipe. The given data is fed to the stdin of the subprocess and the pipe is closed (ie: EOF).

At the same time (as not to cause blocking when dealing with large amounts of data), if stdout-pipe or stderr-pipe were used, reads from those streams. The data that was read is returned in stdout and/or the stderr.

If the subprocess was created with stdout-pipe, stdout-buf will contain the data read from stdout. Otherwise, for subprocesses not created with stdout-pipe, stdout-buf will be set to #f. Similar provisions apply to stderr-buf and stderr-pipe.

As usual, any output variable may be given as #f to ignore it.

If you desire the stdout and stderr data to be interleaved, create the subprocess with stdout-pipe and stderr-merge. The merged result will be returned in stdout-buf and stderr-buf will be set to #f.

In case of any error (including cancellation), #f will be returned with error set. Some or all of the stdin data may have been written. Any stdout or stderr data that has been read will be discarded. None of the out variables (aside from error) will have been set to anything in particular and should not be inspected.

In the case that #t is returned, the subprocess has exited and the exit status inspection APIs (eg: subprocess:get-if-exited?, subprocess:get-exit-status) may be used.

You should not attempt to use any of the subprocess pipes after starting this function, since they may be left in strange states, even if the operation was cancelled. You should especially not attempt to interact with the pipes while the operation is in progress (either from another thread or if using the asynchronous version).

self

Passed: a <GSubprocess>.

a <GSubprocess>

stdin-buf

Passed, or left out: a <GBytes>, or #f.

data to send to the stdin of the subprocess, or #f

cancellable

Passed, or left out: a <GCancellable>, or #f.

a <GCancellable>

%return

Returned: a boolean.

#t if successful

stdout-buf

Returned: a <GBytes>, or #f.

data read from the subprocess stdout

stderr-buf

Returned: a <GBytes>, or #f.

data read from the subprocess stderr

Method: subprocess:communicate-async self [stdin-buf] [cancellable] [callback]
Method: communicate-async self [stdin-buf] [cancellable] [callback]

Asynchronous version of subprocess:communicate. Complete invocation with subprocess:communicate-finish.

self

Passed: a <GSubprocess>.

Self

stdin-buf

Passed, or left out: a <GBytes>, or #f.

Input data, or #f

cancellable

Passed, or left out: a <GCancellable>, or #f.

Cancellable

callback

Passed, or left out: a procedure, or #f.

Callback

See GAsyncReadyCallback for how it is called.

user-data

Not passed: guile-gi supplies it.

User data

Method: subprocess:communicate-finish self result ⇒ %return stdout-buf stderr-buf
Method: communicate-finish self result ⇒ %return stdout-buf stderr-buf

Complete an invocation of subprocess:communicate-async.

self

Passed: a <GSubprocess>.

Self

result

Passed: a <GAsyncResult>.

Result

%return

Returned: a boolean.

stdout-buf

Returned: a <GBytes>, or #f.

Return location for stdout data

stderr-buf

Returned: a <GBytes>, or #f.

Return location for stderr data

Method: subprocess:communicate-utf8 self [stdin-buf] [cancellable] ⇒ %return stdout-buf stderr-buf
Method: communicate-utf8 self [stdin-buf] [cancellable] ⇒ %return stdout-buf stderr-buf

Like subprocess:communicate, but validates the output of the process as UTF-8, and returns it as a regular NUL terminated string.

On error, stdout-buf and stderr-buf will be set to undefined values and should not be used.

self

Passed: a <GSubprocess>.

a <GSubprocess>

stdin-buf

Passed, or left out: a string, or #f.

data to send to the stdin of the subprocess, or #f

cancellable

Passed, or left out: a <GCancellable>, or #f.

a <GCancellable>

%return

Returned: a boolean.

stdout-buf

Returned: a string, or #f.

data read from the subprocess stdout

stderr-buf

Returned: a string, or #f.

data read from the subprocess stderr

Method: subprocess:communicate-utf8-async self [stdin-buf] [cancellable] [callback]
Method: communicate-utf8-async self [stdin-buf] [cancellable] [callback]

Asynchronous version of subprocess:communicate-utf8. Complete invocation with subprocess:communicate-utf8-finish.

self

Passed: a <GSubprocess>.

Self

stdin-buf

Passed, or left out: a string, or #f.

Input data, or #f

cancellable

Passed, or left out: a <GCancellable>, or #f.

Cancellable

callback

Passed, or left out: a procedure, or #f.

Callback

See GAsyncReadyCallback for how it is called.

user-data

Not passed: guile-gi supplies it.

User data

Method: subprocess:communicate-utf8-finish self result ⇒ %return stdout-buf stderr-buf
Method: communicate-utf8-finish self result ⇒ %return stdout-buf stderr-buf

Complete an invocation of subprocess:communicate-utf8-async.

self

Passed: a <GSubprocess>.

Self

result

Passed: a <GAsyncResult>.

Result

%return

Returned: a boolean.

stdout-buf

Returned: a string, or #f.

Return location for stdout data

stderr-buf

Returned: a string, or #f.

Return location for stderr data

Method: subprocess:force-exit self
Method: force-exit self

Use an operating-system specific method to attempt an immediate, forceful termination of the process. There is no mechanism to determine whether or not the request itself was successful; however, you can use subprocess:wait? to monitor the status of the process after calling this function.

On Unix, this function sends SIGKILL.

self

Passed: a <GSubprocess>.

a <GSubprocess>

Method: subprocess:get-exit-status self ⇒ %return
Method: get-exit-status self ⇒ %return

Check the exit status of the subprocess, given that it exited normally. This is the value passed to the exit() system call or the return value from main.

This is equivalent to the system WEXITSTATUS macro.

It is an error to call this function before subprocess:wait? and unless subprocess:get-if-exited? returned #t.

self

Passed: a <GSubprocess>.

a <GSubprocess>

%return

Returned: a 32-bit integer.

the exit status

Method: subprocess:get-identifier self ⇒ %return
Method: get-identifier self ⇒ %return

On UNIX, returns the process ID as a decimal string. On Windows, returns the result of GetProcessId() also as a string. If the subprocess has terminated, this will return #f.

self

Passed: a <GSubprocess>.

a <GSubprocess>

%return

Returned: a string, or #f.

the subprocess identifier, or #f if the subprocess has terminated

Method: subprocess:get-if-exited? self ⇒ %return
Method: get-if-exited? self ⇒ %return

Check if the given subprocess exited normally (ie: by way of exit() or return from main()).

This is equivalent to the system WIFEXITED macro.

It is an error to call this function before subprocess:wait? has returned.

self

Passed: a <GSubprocess>.

a <GSubprocess>

%return

Returned: a boolean.

#t if the case of a normal exit

Method: subprocess:get-if-signaled? self ⇒ %return
Method: get-if-signaled? self ⇒ %return

Check if the given subprocess terminated in response to a signal.

This is equivalent to the system WIFSIGNALED macro.

It is an error to call this function before subprocess:wait? has returned.

self

Passed: a <GSubprocess>.

a <GSubprocess>

%return

Returned: a boolean.

#t if the case of termination due to a signal

Method: subprocess:get-status self ⇒ %return
Method: get-status self ⇒ %return

Gets the raw status code of the process, as from waitpid().

This value has no particular meaning, but it can be used with the macros defined by the system headers such as WIFEXITED. It can also be used with spawn-check-wait-status?.

It is more likely that you want to use subprocess:get-if-exited? followed by subprocess:get-exit-status.

It is an error to call this function before subprocess:wait? has returned.

self

Passed: a <GSubprocess>.

a <GSubprocess>

%return

Returned: a 32-bit integer.

the (meaningless) waitpid() exit status from the kernel

Method: subprocess:get-stderr-pipe self ⇒ %return
Method: get-stderr-pipe self ⇒ %return

Gets the <GInputStream> from which to read the stderr output of self.

The process must have been created with stderr-pipe, otherwise #f will be returned.

self

Passed: a <GSubprocess>.

a <GSubprocess>

%return

Returned: a <GInputStream>, or #f.

the stderr pipe

Method: subprocess:get-stdin-pipe self ⇒ %return
Method: get-stdin-pipe self ⇒ %return

Gets the <GOutputStream> that you can write to in order to give data to the stdin of self.

The process must have been created with stdin-pipe and not stdin-inherit, otherwise #f will be returned.

self

Passed: a <GSubprocess>.

a <GSubprocess>

%return

Returned: a <GOutputStream>, or #f.

the stdout pipe

Method: subprocess:get-stdout-pipe self ⇒ %return
Method: get-stdout-pipe self ⇒ %return

Gets the <GInputStream> from which to read the stdout output of self.

The process must have been created with stdout-pipe, otherwise #f will be returned.

self

Passed: a <GSubprocess>.

a <GSubprocess>

%return

Returned: a <GInputStream>, or #f.

the stdout pipe

Method: subprocess:get-successful? self ⇒ %return
Method: get-successful? self ⇒ %return

Checks if the process was "successful". A process is considered successful if it exited cleanly with an exit status of 0, either by way of the exit() system call or return from main().

It is an error to call this function before subprocess:wait? has returned.

self

Passed: a <GSubprocess>.

a <GSubprocess>

%return

Returned: a boolean.

#t if the process exited cleanly with a exit status of 0

Method: subprocess:get-term-sig self ⇒ %return
Method: get-term-sig self ⇒ %return

Get the signal number that caused the subprocess to terminate, given that it terminated due to a signal.

This is equivalent to the system WTERMSIG macro.

It is an error to call this function before subprocess:wait? and unless subprocess:get-if-signaled? returned #t.

self

Passed: a <GSubprocess>.

a <GSubprocess>

%return

Returned: a 32-bit integer.

the signal causing termination

Procedure: subprocess:new argv flags ⇒ %return

Create a new process with the given flags and varargs argument list. By default, matching the spawn-async defaults, the child’s stdin will be set to the system null device, and stdout/stderr will be inherited from the parent. You can use flags to control this behavior.

The argument list must be terminated with #f.

argv

Passed: a list or vector, each a file name, as a string.

flags

Passed: a <GSubprocessFlags>, or a list of its symbols, or one of them.

flags that define the behaviour of the subprocess

%return

Returned: a <GSubprocess>.

A newly created <GSubprocess>, or #f on error (and error will be set)

error

return location for an error, or #f

argv0

first commandline argument to pass to the subprocess

...

more commandline arguments, followed by #f

Method: subprocess:send-signal self signal-num
Method: send-signal self signal-num

Sends the UNIX signal signal-num to the subprocess, if it is still running.

This API is race-free. If the subprocess has terminated, it will not be signalled.

This API is not available on Windows.

self

Passed: a <GSubprocess>.

a <GSubprocess>

signal-num

Passed: a 32-bit integer.

the signal number to send

Method: subprocess:wait-async self [cancellable] [callback]
Method: wait-async self [cancellable] [callback]

Wait for the subprocess to terminate.

This is the asynchronous version of subprocess:wait?.

self

Passed: a <GSubprocess>.

a <GSubprocess>

cancellable

Passed, or left out: a <GCancellable>, or #f.

a <GCancellable>, or #f

callback

Passed, or left out: a procedure, or #f.

a GAsyncReadyCallback to call when the operation is complete

See GAsyncReadyCallback for how it is called.

user-data

Not passed: guile-gi supplies it.

user_data for callback

Method: subprocess:wait-check-async self [cancellable] [callback]
Method: wait-check-async self [cancellable] [callback]

Combines subprocess:wait-async with spawn-check-wait-status?.

This is the asynchronous version of subprocess:wait-check?.

self

Passed: a <GSubprocess>.

a <GSubprocess>

cancellable

Passed, or left out: a <GCancellable>, or #f.

a <GCancellable>, or #f

callback

Passed, or left out: a procedure, or #f.

a GAsyncReadyCallback to call when the operation is complete

See GAsyncReadyCallback for how it is called.

user-data

Not passed: guile-gi supplies it.

user_data for callback

Method: subprocess:wait-check-finish? self result ⇒ %return
Method: wait-check-finish? self result ⇒ %return

Collects the result of a previous call to subprocess:wait-check-async.

self

Passed: a <GSubprocess>.

a <GSubprocess>

result

Passed: a <GAsyncResult>.

the <GAsyncResult> passed to your GAsyncReadyCallback

%return

Returned: a boolean.

#t if successful, or #f with error set

Method: subprocess:wait-check? self [cancellable] ⇒ %return
Method: wait-check? self [cancellable] ⇒ %return

Combines subprocess:wait? with spawn-check-wait-status?.

self

Passed: a <GSubprocess>.

a <GSubprocess>

cancellable

Passed, or left out: a <GCancellable>, or #f.

a <GCancellable>

%return

Returned: a boolean.

#t on success, #f if process exited abnormally, or cancellable was cancelled

Method: subprocess:wait-finish? self result ⇒ %return
Method: wait-finish? self result ⇒ %return

Collects the result of a previous call to subprocess:wait-async.

self

Passed: a <GSubprocess>.

a <GSubprocess>

result

Passed: a <GAsyncResult>.

the <GAsyncResult> passed to your GAsyncReadyCallback

%return

Returned: a boolean.

#t if successful, or #f with error set

Method: subprocess:wait? self [cancellable] ⇒ %return
Method: wait? self [cancellable] ⇒ %return

Synchronously wait for the subprocess to terminate.

After the process terminates you can query its exit status with functions such as subprocess:get-if-exited? and subprocess:get-exit-status.

This function does not fail in the case of the subprocess having abnormal termination. See subprocess:wait-check? for that.

Cancelling cancellable doesn’t kill the subprocess. Call subprocess:force-exit if it is desirable.

self

Passed: a <GSubprocess>.

a <GSubprocess>

cancellable

Passed, or left out: a <GCancellable>, or #f.

a <GCancellable>

%return

Returned: a boolean.

#t on success, #f if cancellable was cancelled