Debian Codegraph @

doc/modules/guide/pages/cockpit-spawn.adoc

raw
1 = cockpit.js: Spawning Processes
2
3 This is the API for spawning a process and receiving its output, as well
4 as exit codes.
5
6 [[cockpit-spawn-spawn]]
7 == cockpit.spawn()
8
9 ....
10 process = cockpit.spawn(args, [options])
11 ....
12
13 Spawns a process on the system.
14
15 The `+args+` should be an array starting with the executable and
16 containing all the arguments to pass on the command line. If `+args+` is
17 a string then it is interpreted as an executable name. The optional
18 `+options+` argument is a javascript plain object and can contain any of
19 the following fields:
20
21 `+"binary"+`::
22 If set to `+true+` then handle the input and output of the process as
23 arrays of binary bytes.
24 `+"directory"+`::
25 The directory to spawn the process in.
26 `+"err"+`::
27 Controls where the standard error is sent. By default it is logged to
28 the journal. If set to `+"out"+` it is included in with the output
29 data. If set to `+"ignore"+` then the error output is discarded. If
30 set to `+"message"+`, then it will be returned as the error message.
31 When the `+"pty"+` field is set, this field has no effect.
32 `+"environ"+`::
33 An optional array that contains strings to be used as additional
34 environment variables for the new process. These are `+"NAME=VALUE"+`
35 strings.
36 `+"pty"+`::
37 Launch the process in its own PTY terminal, and send/receive terminal
38 input and output.
39 `+"batch"+`::
40 Batch data coming from the process in blocks of at least this size.
41 This is not a guarantee. After a short timeout the data will be sent
42 even if the data doesn't match the batch size. Defaults to zero.
43 `+"latency"+`::
44 The timeout for flushing any cached data in milliseconds.
45 `+"superuser"+`::
46 Set to `+"require"+` to spawn the process as root instead of the
47 logged in user. If the currently logged in user is not permitted to
48 become root (eg: via `+pkexec+`) then the `+client+` will immediately
49 be xref:cockpit-dbus.adoc#cockpit-dbus-onclose[closed] with a `+"access-denied"+`
50 problem code.
51 +
52 Set to `+"try"+` to try to run the process as root, but if that fails,
53 fall back to an unprivileged process.
54
55 The spawned process is a
56 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise[promise]
57 that will complete if the process exits successfully, or fail if there's
58 a problem. Some additional methods besides the standard promise methods
59 are documented below.
60
61 The standard output of the process is made available via the spawned
62 process object. Any non-UTF8 output from the process will be coerced
63 into textual form. It is highly recommended that only textual output be
64 produced by the command. The standard error is logged to the journal.
65
66 [[cockpit-spawn-script]]
67 == cockpit.script()
68
69 ....
70 process = cockpit.script(script, [args], [options])
71 ....
72
73 Run a shell script on the system.
74
75 This function link:#cockpit-spawn-spawn[spawns] a Bourne shell script
76 process. The full text of the `+/bin/sh+` shell script should be passed
77 in as the first argument. The `+args+` can be an array of arguments, not
78 including the executable, which are passed to the script as `+$1+`,
79 `+$2+` and so on. Shebang options are not used or respected.
80
81 The `+options+` is an optional javascript plain object and can include
82 any of the fields listed for the
83 link:#cockpit-spawn-spawn[`+cockpit.spawn()+`] function.
84
85 The spawned process is a
86 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise[promise]
87 that will complete if the script exits successfully, or fail if there's
88 a problem. Some additional methods besides the standard promise methods
89 are documented below.
90
91 The standard output of the process is made available via the spawned
92 process object. Any non-UTF8 output from the process will be coerced
93 into textual form. It is highly recommended that only textual output be
94 produced by the command. The standard error is logged to the journal by
95 default.
96
97 [[cockpit-spawn-then]]
98 == process.then()
99
100 ....
101 process.then((data[, message]) => { ... })
102 ....
103
104 This is a standard
105 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise[promise]
106 method. It sets up a handler to be called when the process finishes
107 successfully.
108
109 The `+data+` argument contains the standard output of the process. If it
110 a string, unless the process was opened in binary mode, in which case
111 the `+data+` is an array of bytes. If a `+process.stream()+` handler is
112 set up, then any standard output data consumed by the handler will not
113 be included in the `+data+` argument.
114
115 If the process was spawned with the `+"err"+` option set to
116 `+"message"+` then the second argument will contain the standard error
117 output of the process.
118
119 [[cockpit-spawn-catch]]
120 == process.catch()
121
122 ....
123 process.catch((exception[, data]) => { ... })
124 ....
125
126 This is a standard
127 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise[Promise]
128 method. It sets up a handler to be called when the process fails,
129 terminates or exits.
130
131 The `+exception+` object passed to the handler can have the following
132 fields:
133
134 `+message+`::
135 A message describing the exception. If the process was spawned with
136 the `+"err"+` option set to `+"message"+` then the second argument
137 will contain the standard error output of the process.
138 `+problem+`::
139 A xref:cockpit-error.adoc#cockpit-problems[problem code] string when a problem occurred
140 starting or communicating with the process. This is `+null+` if the
141 process exited or was terminated.
142 `+exit_status+`::
143 The numeric exit status of the process. This is `+null+` if the
144 process did not exit.
145 `+exit_signal+`::
146 A string representing a unix signal that caused the process to
147 terminate. This is `+null+` if the process did not terminate because
148 of a signal.
149
150 If the process actually ran and produced output before failing, it will
151 be available in the `+data+` argument. Otherwise this argument will be
152 `+undefined+`.
153
154 [[cockpit-spawn-stream]]
155 == process.stream()
156
157 ....
158 process.stream(data => { ... })
159 ....
160
161 This sets up a handler to be called when the process has standard
162 output. The handler will be called multiple times. The handler will be
163 called regardless of whether the process ends up exiting successfully or
164 not.
165
166 Only one handler may be registered at a time. Registering an additional
167 handler replaces the previous one. The handler receives either string
168 `+data+` or an array of binary bytes as its argument. A stream handler
169 may return a number, which indicates the number of characters or bytes
170 consumed from `+data+`. Any data not consumed will be included again the
171 next time the handler is called.
172
173 If a `+process.stream()+` handler is set up, then the `+process.then()+`
174 handlers will only get any remaining data not consumed by the stream
175 handler.
176
177 [[cockpit-spawn-input]]
178 == process.input()
179
180 ....
181 process.input(data, [stream])
182 ....
183
184 This method writes `+data+` to the standard input of the process. If
185 `+data+` is `+null+` or `+undefined+` it is not sent. The `+data+`
186 should be a string or an array of bytes if the process was opened in
187 binary mode.
188
189 If `+stream+` is set to `+true+` then this function may be called again
190 with further input. Otherwise the standard input of the process is
191 closed.
192
193 [[cockpit-spawn-close]]
194 == process.close()
195
196 ....
197 process.close([problem])
198 ....
199
200 Close the process by closing its standard input and output. If
201 `+problem+` is specified it should be a standard
202 xref:cockpit-error.adoc#cockpit-problems[problem code] string. In this case the process
203 will be terminated with a signal.