List, Query, Manipulate System Processes

lifecycle Travis build status AppVeyor build status CRAN status CRAN RStudio mirror downloads Coverage status

ps implements an API to query and manipulate system processes. Most of its code is based on the psutil Python package.

Installation

You can install the released version of ps from CRAN with:

Supported platforms

ps currently supports Windows (from Vista), macOS and Linux systems. On unsupported platforms the package can be installed and loaded, but all of its functions fail with an error of class "not_implemented".

Listing all processes

ps_pids() returns all process ids on the system. This can be useful to iterate over all processes.

library(ps)
ps_pids()[1:20]

ps() returns a data frame (tibble if you have the tibble package available), with data about each process. It contains a handle to each process, in the ps_handle column, you can use these to perform more queries on the processes.

ps()

Process API

This is a short summary of the API. Please see the documentation of the various methods for details, in particular regarding handles to finished processes and pid reuse. See also “Finished and zombie processes” and “pid reuse” below.

ps_handle(pid) creates a process handle for the supplied process id. If pid is omitted, a handle to the calling process is returned:

Query functions

ps_pid(p) returns the pid of the process.

ps_create_time() returns the creation time of the process (according to the OS).

The process id and the creation time uniquely identify a process in a system. ps uses them to make sure that it reports information about, and manipulates the correct process.

ps_is_running(p) returns whether p is still running. It handles pid reuse safely.

ps_ppid(p) returns the pid of the parent of p.

ps_parent(p) returns a process handle to the parent process of p.

ps_name(p) returns the name of the program p is running.

ps_exe(p) returns the full path to the executable the p is running.

ps_cmdline(p) returns the command line (executable and arguments) of p.

ps_status(p) returns the status of the process. Possible values are OS dependent, but typically there is "running" and "stopped".

ps_username(p) returns the name of the user the process belongs to.

ps_uids(p) and ps_gids(p) return the real, effective and saved user ids of the process. They are only implemented on POSIX systems.

if (ps_os_type()[["POSIX"]]) ps_uids(p)
if (ps_os_type()[["POSIX"]]) ps_gids(p)

ps_cwd(p) returns the current working directory of the process.

ps_terminal(p) returns the name of the terminal of the process, if any. For processes without a terminal, and on Windows it returns NA_character_.

ps_environ(p) returns the environment variables of the process. ps_environ_raw(p) does the same, in a different form. Typically they reflect the environment variables at the start of the process.

ps_environ(p)[c("TERM", "USER", "SHELL", "R_HOME")]

ps_num_threads(p) returns the current number of threads of the process.

ps_cpu_times(p) returns the CPU times of the process, similarly to proc.time().

ps_memory_info(p) returns memory usage information. See the manual for details.

ps_children(p) lists all child processes (potentially recuirsively) of the current process.

ps_num_fds(p) returns the number of open file descriptors (handles on Windows):

f <- file(tmp <- tempfile(), "w")
ps_num_fds(p)
close(f)
unlink(tmp)

ps_open_files(p) lists all open files:

f <- file(tmp <- tempfile(), "w")
ps_open_files(p)

Process manipulation

ps_suspend(p) suspends (stops) the process. On POSIX it sends a SIGSTOP signal. On Windows it stops all threads.

ps_resume(p) resumes the process. On POSIX it sends a SIGCONT signal. On Windows it resumes all stopped threads.

ps_send_signal(p) sends a signal to the process. It is implemented on POSIX systems only. It makes an effort to work around pid reuse.

ps_terminate(p) send SIGTERM to the process. On POSIX systems only.

ps_kill(p) terminates the process. Sends SIGKILL on POSIX systems, uses TerminateProcess() on Windows. It make an effort to work around pid reuse.

ps_interrupt(p) interrupts a process. It sends a SIGINT signal on POSIX systems, and it can send a CTRL+C or a CTRL+BREAK event on Windows.

Finished and zombie processes

ps handles finished and Zombie processes as much as possible.

The essential ps_pid(), ps_create_time(), ps_is_running() functions and the format() and print() methods work for all processes, including finished and zombie processes. Other functions fail with an error of class "no_such_process" for finished processes.

The ps_ppid(), ps_parent(), ps_children(), ps_name(), ps_status(), ps_username(), ps_uids(), ps_gids(), ps_terminal(), ps_children() and the signal sending functions work properly for zombie processes. Other functions fail with "zombie_process" error.

Pid reuse

ps functions handle pid reuse as well as technically possible.

The query functions never return information about the wrong process, even if the process has finished and its process id was re-assigned.

On Windows, the process manipulation functions never manipulate the wrong process.

On POSIX systems, this is technically impossible, it is not possible to send a signal to a process without creating a race condition. In ps the time window of the race condition is very small, a few microseconds, and the process would need to finish, and the OS would need to reuse its pid within this time window to create problems. This is very unlikely to happen.

Recipes

In the spirit of psutil recipes.

Wait for a process to finish

On POSIX, there is no good way to wait for non-child processes to finish, so we need to write a sleep-wait loop to do it. (On Windows, and BSD systems, including macOS, there are better solutions.)

Terminate children

Note, that some R IDEs, including RStudio, run a multithreaded R process, and other threads may start processes as well. reap_children() will clean up all these as well, potentially causing the IDE to misbehave or crash.

Filtering and sorting processes

Process name ending with “sh”:

ps() %>%
  filter(grepl("sh$", name))

Processes owned by user:

ps() %>%
  filter(username == Sys.info()[["user"]]) %>%
  select(pid, name)

Processes consuming more than 100MB of memory:

ps() %>%
  filter(rss > 100 * 1024 * 1024)
## # A tibble: 16 x 11
##      pid  ppid name    username  status    user  system    rss    vms created             ps_handle
##    <int> <int> <chr>   <chr>     <chr>    <dbl>   <dbl>  <dbl>  <dbl> <dttm>              <I(list)>
##  1 96817  3718 Google… gaborcsa… runni… 9.53e-1 1.63e-1 1.14e8 3.56e9 2018-07-24 09:09:42 <S3: ps_…
##  2 96816  3718 Google… gaborcsa… runni… 1.58e+0 2.65e-1 1.45e8 3.66e9 2018-07-24 09:09:37 <S3: ps_…
##  3 96809  3718 Google… gaborcsa… runni… 8.65e-1 1.58e-1 1.41e8 3.59e9 2018-07-24 09:09:34 <S3: ps_…
##  4 96680  3718 Google… gaborcsa… runni… 1.25e+1 1.86e+0 2.02e8 3.54e9 2018-07-24 08:41:27 <S3: ps_…
##  5 96679  3718 Google… gaborcsa… runni… 2.65e+1 5.93e+0 1.48e8 3.58e9 2018-07-24 08:41:27 <S3: ps_…
##  6 96678  3718 Google… gaborcsa… runni… 1.71e+2 1.61e+1 2.72e8 3.70e9 2018-07-24 08:41:26 <S3: ps_…
##  7 96674  3718 Google… gaborcsa… runni… 1.04e+1 2.29e+0 1.79e8 3.63e9 2018-07-24 08:41:26 <S3: ps_…
##  8 96673  3718 Google… gaborcsa… runni… 1.06e+2 1.55e+1 1.71e8 3.62e9 2018-07-24 08:41:25 <S3: ps_…
##  9 95883 95882 Virtua… gaborcsa… runni… 2.03e+3 1.32e+3 4.73e9 7.53e9 2018-07-23 22:50:14 <S3: ps_…
## 10 93065 90285 R       gaborcsa… runni… 8.77e+0 1.53e+0 1.33e8 2.75e9 2018-07-23 17:27:55 <S3: ps_…
## 11 90173 90051 Emacs-… gaborcsa… runni… 3.18e+2 5.53e+1 1.99e8 2.89e9 2018-07-23 16:14:37 <S3: ps_…
## 12 92685  3718 Google… gaborcsa… runni… 1.20e+3 1.48e+2 5.32e8 4.89e9 2018-07-22 07:19:55 <S3: ps_…
## 13 35685  3718 Google… gaborcsa… runni… 7.01e+2 7.20e+1 3.67e8 4.15e9 2018-07-19 08:42:23 <S3: ps_…
## 14  3722  3718 Google… gaborcsa… runni… 2.84e+3 1.36e+3 1.44e8 3.40e9 2018-07-17 21:26:21 <S3: ps_…
## 15  3718     1 Google… gaborcsa… runni… 1.14e+4 3.85e+3 4.69e8 4.44e9 2018-07-17 21:26:21 <S3: ps_…
## 16   722     1 iTerm2  gaborcsa… runni… 6.36e+3 1.07e+3 2.24e8 3.59e9 2018-07-17 20:26:27 <S3: ps_…

Top 3 memory consuming processes:

Top 3 processes which consumed the most CPU time:

Contributions

Please note that this project is released with a Contributor Code of Conduct. By participating in this project you agree to abide by its terms.

License

BSD © RStudio