R: Mark a process and its (future) child tree

ps_mark_tree {ps} | R Documentation

## Mark a process and its (future) child tree

### Description

`ps_mark_tree()` generates a random environment variable name and sets it in the current R process. This environment variable will be (by default) inherited by all child (and grandchild, etc.) processes, and will help finding these processes, even if and when they are (no longer) related to the current R process. (I.e. they are not connected in the process tree.)

### Usage

    ps_mark_tree()

    with_process_cleanup(expr)

    ps_find_tree(marker)

    ps_kill_tree(marker, sig = signals()$SIGKILL)

### Arguments

`expr` | R expression to evaluate in the new context.
`marker` | String scalar, the name of the environment variable to use to find the marked processes.
`sig` | The signal to send to the marked processes on Unix. On Windows this argument is ignored currently.

### Details

`ps_find_tree()` finds the processes that set the supplied environment variable and returns them in a list.

`ps_kill_tree()` finds the processes that set the supplied environment variable, and kills them (or sends them the specified signal on Unix).

`with_process_cleanup()` evaluates an R expression, and cleans up all external processes that were started by the R process while evaluating the expression. This includes child processes of child processes, etc., recursively. It returns a list with entries: `result` is the result of the expression, `visible` is TRUE if the expression should be printed to the screen, and `process_cleanup` is a named integer vector of the cleaned pids, names are the process names.

If `expr` throws an error, then so does `with_process_cleanup()`, the same error. Nevertheless processes are still cleaned up.

### Value

`ps_mark_tree()` returns the name of the environment variable, which can be used as the `marker` in `ps_kill_tree()`.

`ps_find_tree()` returns a list of `ps_handle` objects.

`ps_kill_tree()` returns the pids of the killed processes, in a named integer vector. The names are the file names of the executables, when available.

`with_process_cleanup()` returns the value of the evaluated expression.

[Package ps version 1.1.0 Index]