Cockpit Guide |
---|
cockpit.js: Spawning Processescockpit.js: Spawning Processes — Spawning processes or scripts |
This is the API for spawning a process and receiving its output, as well as exit codes.
process = cockpit.spawn(args, [options])
Spawns a process on the system.
The args
should be an array starting with the executable and
containing all the arguments to pass on the command line. If args
is a string then it is interpreted as an executable name. The optional
options
argument is a javascript plain object and can contain
any of the following fields:
|
If set to |
|
The directory to spawn the process in. |
|
Controls where the standard error is sent. By default it is logged
to the journal. If set to |
|
The remote host to spawn the process on. If an alternate user or port is
required it can be specified as |
|
An optional array that contains strings to be used as
additional environment variables for the new process. These are
|
|
Launch the process in its own PTY terminal, and send/receive terminal input and output. |
|
Batch data coming from the process in blocks of at least this size. This is not a guarantee. After a short timeout the data will be sent even if the data doesn't match the batch size. Defaults to zero. |
|
The timeout for flushing any cached data in milliseconds. |
|
Set to Set to |
The spawned process is a promise that will complete if the process exits successfully, or fail if there's a problem. Some additional methods besides the standard promise methods are documented below.
The standard output of the process is made available via the spawned process object. Any non-UTF8 output from the process will be coerced into textual form. It is highly recommended that only textual output be produced by the command. The standard error is logged to the journal.
process = cockpit.script(script, [args], [options])
Run a shell script on the system.
This function spawns a Bourne shell script
process. The full text of the /bin/sh
shell script should be passed in as
the first argument. The args
can be an array of arguments, not including
the executable, which are passed to the script as $1
, $2
and
so on. Shebang options are not used or respected.
The options
is an optional javascript plain object and can include
any of the fields listed for the
cockpit.spawn()
function.
The spawned process is a promise that will complete if the script exits successfully, or fail if there's a problem. Some additional methods besides the standard promise methods are documented below.
The standard output of the process is made available via the spawned process object. Any non-UTF8 output from the process will be coerced into textual form. It is highly recommended that only textual output be produced by the command. The standard error is logged to the journal by default.
process.done(function(data[, message]) { ... })
This is a standard promise method. It sets up a handler to be called when the process finishes successfully.
The data
argument contains the standard output of the process.
If it a string, unless the process was opened in binary mode, in which case the
data
is an array of bytes. If a
process.stream()
handler is set up, then any standard output data consumed by the handler will not
be included in the data
argument.
If the process was spawned with the "err"
option set to
"message"
then the second argument will contain the standard error
output of the process.
process.fail(function(exception[, data]) { ... })
This is a standard jQuery promise method. It sets up a handler to be called when the process fails, terminates or exits.
The exception
object passed to the handler can have the
following fields:
|
A message describing the exception. If the process was spawned with
the |
|
A problem code string when
a problem occurred starting or communicating with the process. This is |
|
The numeric exit status of the process. This is |
|
A string representing a unix signal that caused the process to terminate.
This is |
If the process actually ran and produced output before failing, it will be available in
the data
argument. Otherwise this argument will be undefined
.
process.always(function() { ... })
This is a standard jQuery promise method. It sets up a handler to be called when the process completes, whether it exits successfully, fails, terminates, or exits with a failure.
process.stream(function(data) { ... })
This sets up a handler to be called when the process has standard output. The handler will be called multiple times. The handler will be called regardless of whether the process ends up exiting successfully or not.
Only one handler may be registered at a time. Registering an additional handler
replaces the previous one. The handler receives either string data
or
an array of binary bytes as its argument. A stream handler may return a number, which
indicates the number of characters or bytes consumed from data
. Any data
not consumed will be included again the next time the handler is called.
If a process.stream()
handler is set up, then the
process.done()
handlers will
only get any remaining data not consumed by the stream handler.
process.input(data, [stream])
This method writes data
to the standard input of the process.
If data
is null
or undefined
it is not sent.
The data
should be a string or an array of bytes if the process was
opened in binary mode.
If stream
is set to true
then this function may be
called again with further input. Otherwise the standard input of the process
is closed.
process.close([problem])
Close the process by closing its standard input and output. If problem
is
specified it should be a standard
problem code string. In this case the
process will be terminated with a signal.