File: README.md

package info (click to toggle)
r-cran-processx 3.8.6-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 1,068 kB
  • sloc: ansic: 6,485; sh: 13; makefile: 2
file content (646 lines) | stat: -rw-r--r-- 18,641 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646

# processx

> Execute and Control System Processes

<!-- badges: start -->

[![lifecycle](https://lifecycle.r-lib.org/articles/figures/lifecycle-stable.svg)](https://lifecycle.r-lib.org/articles/stages.html)
[![R-CMD-check](https://github.com/r-lib/processx/actions/workflows/R-CMD-check.yaml/badge.svg)](https://github.com/r-lib/processx/actions/workflows/R-CMD-check.yaml)
[![](https://www.r-pkg.org/badges/version/processx)](https://www.r-pkg.org/pkg/processx)
[![CRAN RStudio mirror
downloads](https://cranlogs.r-pkg.org/badges/processx)](https://www.r-pkg.org/pkg/processx)
[![Codecov test
coverage](https://codecov.io/gh/r-lib/processx/branch/main/graph/badge.svg)](https://app.codecov.io/gh/r-lib/processx?branch=main)
<!-- badges: end -->

Tools to run system processes in the background, read their standard
output and error and kill them.

processx can poll the standard output and error of a single process, or
multiple processes, using the operating system’s polling and waiting
facilities, with a timeout.

------------------------------------------------------------------------

-   [Features](#features)
-   [Installation](#installation)
-   [Usage](#usage)
    -   [Running an external process](#running-an-external-process)
        -   [Errors](#errors)
        -   [Showing output](#showing-output)
        -   [Spinner](#spinner)
        -   [Callbacks for I/O](#callbacks-for-io)
    -   [Managing external processes](#managing-external-processes)
        -   [Starting processes](#starting-processes)
        -   [Killing a process](#killing-a-process)
        -   [Standard output and error](#standard-output-and-error)
        -   [End of output](#end-of-output)
        -   [Polling the standard output and
            error](#polling-the-standard-output-and-error)
        -   [Polling multiple processes](#polling-multiple-processes)
        -   [Waiting on a process](#waiting-on-a-process)
        -   [Exit statuses](#exit-statuses)
        -   [Mixing processx and the parallel base R
            package](#mixing-processx-and-the-parallel-base-r-package)
        -   [Errors](#errors-1)
-   [Related tools](#related-tools)
-   [Code of Conduct](#code-of-conduct)
-   [License](#license)

## Features

-   Start system processes in the background and find their process id.
-   Read the standard output and error, using non-blocking connections
-   Poll the standard output and error connections of a single process
    or multiple processes.
-   Write to the standard input of background processes.
-   Check if a background process is running.
-   Wait on a background process, or multiple processes, with a timeout.
-   Get the exit status of a background process, if it has already
    finished.
-   Kill background processes.
-   Kill background process, when its associated object is garbage
    collected.
-   Kill background processes and all their child processes.
-   Works on Linux, macOS and Windows.
-   Lightweight, it only depends on the also lightweight R6 and ps
    packages.

## Installation

Install the stable version from CRAN:

``` r
install.packages("processx")
```

If you need the development version, install it from GitHub:

``` r
pak::pak("r-lib/processx")
```

## Usage

``` r
library(processx)
```

> Note: the following external commands are usually present in macOS and
> Linux systems, but not necessarily on Windows. We will also use the
> `px` command line tool (`px.exe` on Windows), that is a very simple
> program that can produce output to `stdout` and `stderr`, with the
> specified timings.

``` r
px <- paste0(
  system.file(package = "processx", "bin", "px"),
  system.file(package = "processx", "bin", .Platform$r_arch, "px.exe")
)
px
```

    #> [1] "/Users/gaborcsardi/Library/R/arm64/4.2/library/processx/bin/px"

### Running an external process

The `run()` function runs an external command. It requires a single
command, and a character vector of arguments. You don’t need to quote
the command or the arguments, as they are passed directly to the
operating system, without an intermediate shell.

``` r
run("echo", "Hello R!")
```

    #> $status
    #> [1] 0
    #> 
    #> $stdout
    #> [1] "Hello R!\n"
    #> 
    #> $stderr
    #> [1] ""
    #> 
    #> $timeout
    #> [1] FALSE

Short summary of the `px` binary we are using extensively below:

``` r
result <- run(px, "--help", echo = TRUE)
```

    #> Usage: px [command arg] [command arg] ...
    #> 
    #> Commands:
    #>   sleep  <seconds>           -- sleep for a number os seconds
    #>   out    <string>            -- print string to stdout
    #>   err    <string>            -- print string to stderr
    #>   outln  <string>            -- print string to stdout, add newline
    #>   errln  <string>            -- print string to stderr, add newline
    #>   errflush                   -- flush stderr stream
    #>   cat    <filename>          -- print file to stdout
    #>   return <exitcode>          -- return with exitcode
    #>   writefile <path> <string>  -- write to file
    #>   write <fd> <string>        -- write to file descriptor
    #>   echo <fd1> <fd2> <nbytes>  -- echo from fd to another fd
    #>   getenv <var>               -- environment variable to stdout

> Note: From version 3.0.1, processx does not let you specify a full
> shell command line, as this involves starting a grandchild process
> from the child process, and it is difficult to clean up the grandchild
> process when the child process is killed. The user can still start a
> shell (`sh` or `cmd.exe`) directly of course, and then proper cleanup
> is the user’s responsibility.

#### Errors

By default `run()` throws an error if the process exits with a non-zero
status code. To avoid this, specify `error_on_status = FALSE`:

``` r
run(px, c("out", "oh no!", "return", "2"), error_on_status = FALSE)
```

    #> $status
    #> [1] 2
    #> 
    #> $stdout
    #> [1] "oh no!"
    #> 
    #> $stderr
    #> [1] ""
    #> 
    #> $timeout
    #> [1] FALSE

#### Showing output

To show the output of the process on the screen, use the `echo`
argument. Note that the order of `stdout` and `stderr` lines may be
incorrect, because they are coming from two different connections.

``` r
result <- run(px,
  c("outln", "out", "errln", "err", "outln", "out again"),
  echo = TRUE)
```

    #> out
    #> out again
    #> err

If you have a terminal that support ANSI colors, then the standard error
output is shown in red.

The standard output and error are still included in the result of the
`run()` call:

``` r
result
```

    #> $status
    #> [1] 0
    #> 
    #> $stdout
    #> [1] "out\nout again\n"
    #> 
    #> $stderr
    #> [1] "err\n"
    #> 
    #> $timeout
    #> [1] FALSE

Note that `run()` is different from `system()`, and it always shows the
output of the process on R’s proper standard output, instead of writing
to the terminal directly. This means for example that you can capture
the output with `capture.output()` or use `sink()`, etc.:

``` r
out1 <- capture.output(r1 <- system("ls"))
out2 <- capture.output(r2 <- run("ls", echo = TRUE))
```

``` r
out1
```

    #> character(0)

``` r
out2
```

    #>  [1] "CODE_OF_CONDUCT.md" "DESCRIPTION"        "LICENSE"           
    #>  [4] "LICENSE.md"         "Makefile"           "NAMESPACE"         
    #>  [7] "NEWS.md"            "R"                  "README.Rmd"        
    #> [10] "README.md"          "_pkgdown.yml"       "codecov.yml"       
    #> [13] "inst"               "man"                "processx.Rproj"    
    #> [16] "src"                "tests"

#### Spinner

The `spinner` option of `run()` puts a calming spinner to the terminal
while the background program is running. The spinner is always shown in
the first character of the last line, so you can make it work nicely
with the regular output of the background process if you like. E.g. try
this in your R terminal:

    result <- run(px,
      c("out", "  foo",
        "sleep", "1",
        "out", "\r  bar",
        "sleep", "1",
        "out", "\rX foobar\n"),
      echo = TRUE, spinner = TRUE)

#### Callbacks for I/O

`run()` can call an R function for each line of the standard output or
error of the process, just supply the `stdout_line_callback` or the
`stderr_line_callback` arguments. The callback functions take two
arguments, the first one is a character scalar, the output line. The
second one is the `process` object that represents the background
process. (See more below about `process` objects.) You can manipulate
this object in the callback, if you want. For example you can kill it in
response to an error or some text on the standard output:

``` r
cb <- function(line, proc) {
  cat("Got:", line, "\n")
  if (line == "done") proc$kill()
}
result <- run(px,
  c("outln", "this", "outln", "that", "outln", "done",
    "outln", "still here", "sleep", "10", "outln", "dead by now"), 
  stdout_line_callback = cb,
  error_on_status = FALSE,
)
```

    #> Got: this 
    #> Got: that 
    #> Got: done 
    #> Got: still here

``` r
result
```

    #> $status
    #> [1] -9
    #> 
    #> $stdout
    #> [1] "this\nthat\ndone\nstill here\n"
    #> 
    #> $stderr
    #> [1] ""
    #> 
    #> $timeout
    #> [1] FALSE

Keep in mind, that while the R callback is running, the background
process is not stopped, it is also running. In the previous example,
whether `still here` is printed or not depends on the scheduling of the
R process and the background process by the OS. Typically, it is
printed, because the R callback takes a while to run.

In addition to the line-oriented callbacks, the `stdout_callback` and
`stderr_callback` arguments can specify callback functions that are
called with output chunks instead of single lines. A chunk may contain
multiple lines (separated by `\n` or `\r\n`), or even incomplete lines.

### Managing external processes

If you need better control over possibly multiple background processes,
then you can use the R6 `process` class directly.

#### Starting processes

To start a new background process, create a new instance of the
`process` class.

``` r
p <- process$new("sleep", "20")
```

#### Killing a process

A process can be killed via the `kill()` method.

``` r
p$is_alive()
```

    #> [1] TRUE

``` r
p$kill()
```

    #> [1] TRUE

``` r
p$is_alive()
```

    #> [1] FALSE

Note that processes are finalized (and killed) automatically if the
corresponding `process` object goes out of scope, as soon as the object
is garbage collected by R:

``` r
p <- process$new("sleep", "20")
rm(p)
invisible(gc())
```

Here, the direct call to the garbage collector kills the `sleep` process
as well. See the `cleanup` option if you want to avoid this behavior.

#### Standard output and error

By default the standard output and error of the processes are ignored.
You can set the `stdout` and `stderr` constructor arguments to a file
name, and then they are redirected there, or to `"|"`, and then processx
creates connections to them. (Note that starting from processx 3.0.0
these connections are not regular R connections, because the public R
connection API was retroactively removed from R.)

The `read_output_lines()` and `read_error_lines()` methods can be used
to read complete lines from the standard output or error connections.
They work similarly to the `readLines()` base R function.

Note, that the connections have a buffer, which can fill up, if R does
not read out the output, and then the process will stop, until R reads
the connection and the buffer is freed.

> **Always make sure that you read out the standard output and/or
> error** **of the pipes, otherwise the background process will stop
> running!**

If you don’t need the standard output or error any more, you can also
close it, like this:

``` r
close(p$get_output_connection())
close(p$get_error_connection())
```

Note that the connections used for reading the output and error streams
are non-blocking, so the read functions will return immediately, even if
there is no text to read from them. If you want to make sure that there
is data available to read, you need to poll, see below.

``` r
p <- process$new(px,
  c("sleep", "1", "outln", "foo", "errln", "bar", "outln", "foobar"),
  stdout = "|", stderr = "|")
p$read_output_lines()
```

    #> character(0)

``` r
p$read_error_lines()
```

    #> character(0)

#### End of output

The standard R way to query the end of the stream for a non-blocking
connection, is to use the `isIncomplete()` function. *After a read
attempt*, this function returns `FALSE` if the connection has surely no
more data. (If the read attempt returns no data, but `isIncomplete()`
returns `TRUE`, then the connection might deliver more data in the
future.

The `is_incomplete_output()` and `is_incomplete_error()` functions work
similarly for `process` objects.

#### Polling the standard output and error

The `poll_io()` method waits for data on the standard output and/or
error of a process. It will return if any of the following events
happen:

-   data is available on the standard output of the process (assuming
    there is a connection to the standard output).
-   data is available on the standard error of the process (assuming the
    is a connection to the standard error).
-   The process has finished and the standard output and/or error
    connections were closed on the other end.
-   The specified timeout period expired.

For example the following code waits about a second for output.

``` r
p <- process$new(px, c("sleep", "1", "outln", "kuku"), stdout = "|")

## No output yet
p$read_output_lines()
```

    #> character(0)

``` r
## Wait at most 5 sec
p$poll_io(5000)
```

    #>   output    error  process 
    #>  "ready" "nopipe" "nopipe"

``` r
## There is output now
p$read_output_lines()
```

    #> [1] "kuku"

#### Polling multiple processes

If you need to manage multiple background processes, and need to wait
for output from all of them, processx defines a `poll()` function that
does just that. It is similar to the `poll_io()` method, but it takes
multiple process objects, and returns as soon as one of them have data
on standard output or error, or a timeout expires. Here is an example:

``` r
p1 <- process$new(px, c("sleep", "1", "outln", "output"), stdout = "|")
p2 <- process$new(px, c("sleep", "2", "errln", "error"), stderr = "|")

## After 100ms no output yet
poll(list(p1 = p1, p2 = p2), 100)
```

    #> $p1
    #>    output     error   process 
    #> "timeout"  "nopipe"  "nopipe" 
    #> 
    #> $p2
    #>    output     error   process 
    #>  "nopipe" "timeout"  "nopipe"

``` r
## But now we surely have something
poll(list(p1 = p1, p2 = p2), 1000)
```

    #> $p1
    #>   output    error  process 
    #>  "ready" "nopipe" "nopipe" 
    #> 
    #> $p2
    #>   output    error  process 
    #> "nopipe" "silent" "nopipe"

``` r
p1$read_output_lines()
```

    #> [1] "output"

``` r
## Done with p1
close(p1$get_output_connection())
```

    #> NULL

``` r
## The second process should have data on stderr soonish
poll(list(p1 = p1, p2 = p2), 5000)
```

    #> $p1
    #>   output    error  process 
    #> "closed" "nopipe" "nopipe" 
    #> 
    #> $p2
    #>   output    error  process 
    #> "nopipe"  "ready" "nopipe"

``` r
p2$read_error_lines()
```

    #> [1] "error"

#### Waiting on a process

As seen before, `is_alive()` checks if a process is running. The
`wait()` method can be used to wait until it has finished (or a
specified timeout expires).. E.g. in the following code `wait()` needs
to wait about 2 seconds for the `sleep` `px` command to finish.

``` r
p <- process$new(px, c("sleep", "2"))
p$is_alive()
```

    #> [1] TRUE

``` r
Sys.time()
```

    #> [1] "2022-06-10 13:57:49 CEST"

``` r
p$wait()
Sys.time()
```

    #> [1] "2022-06-10 13:57:51 CEST"

It is safe to call `wait()` multiple times:

``` r
p$wait() # already finished!
```

#### Exit statuses

After a process has finished, its exit status can be queried via the
`get_exit_status()` method. If the process is still running, then this
method returns `NULL`.

``` r
p <- process$new(px, c("sleep", "2"))
p$get_exit_status()
```

    #> NULL

``` r
p$wait()
p$get_exit_status()
```

    #> [1] 0

#### Mixing processx and the parallel base R package

In general, mixing processx (via callr or not) and parallel works fine.
If you use parallel’s ‘fork’ clusters, e.g. via
`parallel::mcparallel()`, then you might see two issues. One is that
processx will not be able to determine the exit status of some processx
processes. This is because the status is read out by parallel, and
processx will set it to `NA`. The other one is that parallel might
complain that it could not clean up some subprocesses. This is not an
error, and it is harmless, but it does hold up R for about 10 seconds,
before parallel gives up. To work around this, you can set the
`PROCESSX_NOTIFY_OLD_SIGCHLD` environment variable to a non-empty value,
before you load processx. This behavior might be the default in the
future.

#### Errors

Errors are typically signalled via non-zero exits statuses. The processx
constructor fails if the external program cannot be started, but it does
not deal with errors that happen after the program has successfully
started running.

``` r
p <- process$new("nonexistant-command-for-sure")
```

    #> Error in c("process_initialize(self, private, command, args, stdin, stdout, ", : ! Native call to `processx_exec` failed
    #> Caused by error in `chain_call(c_processx_exec, command, c(command, args), pty, pty_options, …` at initialize.R:138:3:
    #> ! cannot start processx process 'nonexistant-command-for-sure' (system error 2, No such file or directory) @unix/processx.c:613 (processx_exec)

``` r
p2 <- process$new(px, c("sleep", "1", "command-does-not-exist"))
p2$wait()
p2$get_exit_status()
```

    #> [1] 5

## Related tools

-   The [`ps` package](https://ps.r-lib.org/) can query, list,
    manipulate all system processes (not just subprocesses), and
    processx uses it internally for some of its functionality. You can
    also convert a `processx::process` object to a `ps::ps_handle` with
    the `as_ps_handle()` method.

-   The [`callr` package](https://callr.r-lib.org/) uses processx to
    start another R process, and run R code in it, in the foreground or
    background.

## Code of Conduct

Please note that the processx project is released with a [Contributor
Code of Conduct](https://processx.r-lib.org/CODE_OF_CONDUCT.html). By
contributing to this project, you agree to abide by its terms.

## License

MIT © Ascent Digital Services, RStudio, Gábor Csárdi