= cockpit.js: Spawning ProcessesThis is the API for spawning a process and receiving its output, as wellas exit codes.[[cockpit-spawn-spawn]]== cockpit.spawn()....process = cockpit.spawn(args, [options])....Spawns a process on the system.The `+args+` should be an array starting with the executable andcontaining all the arguments to pass on the command line. If `+args+` isa string then it is interpreted as an executable name. The optional`+options+` argument is a javascript plain object and can contain any ofthe following fields:`+"binary"+`::If set to `+true+` then handle the input and output of the process asarrays of binary bytes.`+"directory"+`::The directory to spawn the process in.`+"err"+`::Controls where the standard error is sent. By default it is logged tothe journal. If set to `+"out"+` it is included in with the outputdata. If set to `+"ignore"+` then the error output is discarded. Ifset to `+"message"+`, then it will be returned as the error message.When the `+"pty"+` field is set, this field has no effect.`+"environ"+`::An optional array that contains strings to be used as additionalenvironment variables for the new process. These are `+"NAME=VALUE"+`strings.`+"pty"+`::Launch the process in its own PTY terminal, and send/receive terminalinput and output.`+"batch"+`::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 senteven if the data doesn't match the batch size. Defaults to zero.`+"latency"+`::The timeout for flushing any cached data in milliseconds.`+"superuser"+`::Set to `+"require"+` to spawn the process as root instead of thelogged in user. If the currently logged in user is not permitted tobecome root (eg: via `+pkexec+`) then the `+client+` will immediatelybe xref:cockpit-dbus.adoc#cockpit-dbus-onclose[closed] with a `+"access-denied"+`problem code.+Set to `+"try"+` to try to run the process as root, but if that fails,fall back to an unprivileged process.The spawned process is ahttps://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise[promise]that will complete if the process exits successfully, or fail if there'sa problem. Some additional methods besides the standard promise methodsare documented below.The standard output of the process is made available via the spawnedprocess object. Any non-UTF8 output from the process will be coercedinto textual form. It is highly recommended that only textual output beproduced by the command. The standard error is logged to the journal.[[cockpit-spawn-script]]== cockpit.script()....process = cockpit.script(script, [args], [options])....Run a shell script on the system.This function link:#cockpit-spawn-spawn[spawns] a Bourne shell scriptprocess. The full text of the `+/bin/sh+` shell script should be passedin as the first argument. The `+args+` can be an array of arguments, notincluding 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 includeany of the fields listed for thelink:#cockpit-spawn-spawn[`+cockpit.spawn()+`] function.The spawned process is ahttps://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise[promise]that will complete if the script exits successfully, or fail if there'sa problem. Some additional methods besides the standard promise methodsare documented below.The standard output of the process is made available via the spawnedprocess object. Any non-UTF8 output from the process will be coercedinto textual form. It is highly recommended that only textual output beproduced by the command. The standard error is logged to the journal bydefault.[[cockpit-spawn-then]]== process.then()....process.then((data[, message]) => { ... })....This is a standardhttps://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise[promise]method. It sets up a handler to be called when the process finishessuccessfully.The `+data+` argument contains the standard output of the process. If ita string, unless the process was opened in binary mode, in which casethe `+data+` is an array of bytes. If a `+process.stream()+` handler isset up, then any standard output data consumed by the handler will notbe 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 erroroutput of the process.[[cockpit-spawn-catch]]== process.catch()....process.catch((exception[, data]) => { ... })....This is a standardhttps://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise[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 followingfields:`+message+`::A message describing the exception. If the process was spawned withthe `+"err"+` option set to `+"message"+` then the second argumentwill contain the standard error output of the process.`+problem+`::A xref:cockpit-error.adoc#cockpit-problems[problem code] string when a problem occurredstarting or communicating with the process. This is `+null+` if theprocess exited or was terminated.`+exit_status+`::The numeric exit status of the process. This is `+null+` if theprocess did not exit.`+exit_signal+`::A string representing a unix signal that caused the process toterminate. This is `+null+` if the process did not terminate becauseof a signal.If the process actually ran and produced output before failing, it willbe available in the `+data+` argument. Otherwise this argument will be`+undefined+`.[[cockpit-spawn-stream]]== process.stream()....process.stream(data => { ... })....This sets up a handler to be called when the process has standardoutput. The handler will be called multiple times. The handler will becalled regardless of whether the process ends up exiting successfully ornot.Only one handler may be registered at a time. Registering an additionalhandler replaces the previous one. The handler receives either string`+data+` or an array of binary bytes as its argument. A stream handlermay return a number, which indicates the number of characters or bytesconsumed from `+data+`. Any data not consumed will be included again thenext time the handler is called.If a `+process.stream()+` handler is set up, then the `+process.then()+`handlers will only get any remaining data not consumed by the streamhandler.[[cockpit-spawn-input]]== process.input()....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 inbinary mode.If `+stream+` is set to `+true+` then this function may be called againwith further input. Otherwise the standard input of the process isclosed.[[cockpit-spawn-close]]== process.close()....process.close([problem])....Close the process by closing its standard input and output. If`+problem+` is specified it should be a standardxref:cockpit-error.adoc#cockpit-problems[problem code] string. In this case the processwill be terminated with a signal.