Skip to content

ps_environ() returns the environment variables of the process, in a named vector, similarly to the return value of Sys.getenv() (without arguments).

Usage

ps_environ(p = ps_handle())

ps_environ_raw(p = ps_handle())

Arguments

p

Process handle.

Value

ps_environ() returns a named character vector (that has a Dlist class, so it is printed nicely), ps_environ_raw() returns a character vector.

Details

Note: this usually does not reflect changes made after the process started.

ps_environ_raw() is similar to p$environ() but returns the unparsed "var=value" strings. This is faster, and sometimes good enough.

These functions throw a zombie_process error for zombie processes.

macOS issues

ps_environ() usually does not work on macOS nowadays. This is because macOS does not allow reading the environment variables of another process. Accoding to the Darwin source code, ps_environ will work is one of these conditions hold:

  • You are running a development or debug kernel, i.e. if you are debugging the macOS kernel itself.

  • The target process is same as the calling process.

  • SIP if off.

  • The target process is not restricted, e.g. it is running a binary that was not signed.

  • The calling process has the com.apple.private.read-environment-variables entitlement. However adding this entitlement to the R binary makes R crash on startup.

Otherwise ps_environ will return an empty set of environment variables on macOS.

Issue 121 might have more information about this.

Examples

p <- ps_handle()
p
#> <ps::ps_handle> PID=6015, NAME=R, AT=2024-10-29 08:51:51.83
env <- ps_environ(p)
env[["R_HOME"]]
#> [1] "/opt/R/4.4.1/lib/R"