ps

R package to query, list, manipulate system processes

https://github.com/r-lib/ps

Science Score: 36.0%

This score indicates how likely this project is to be science-related based on various indicators:

  • CITATION.cff file
  • codemeta.json file
    Found codemeta.json file
  • .zenodo.json file
    Found .zenodo.json file
  • DOI references
  • Academic publication links
  • Committers with academic emails
    1 of 16 committers (6.3%) from academic institutions
  • Institutional organization owner
  • JOSS paper metadata
  • Scientific vocabulary similarity
    Low similarity (13.9%) to scientific vocabulary

Keywords from Contributors

devtools tidy-data visualisation date-time excel strings unit-testing parsing setup data-manipulation
Last synced: 10 months ago · JSON representation

Repository

R package to query, list, manipulate system processes

Basic Info
  • Host: GitHub
  • Owner: r-lib
  • License: other
  • Language: C
  • Default Branch: main
  • Homepage: https://ps.r-lib.org/
  • Size: 9.22 MB
Statistics
  • Stars: 81
  • Watchers: 2
  • Forks: 21
  • Open Issues: 9
  • Releases: 23
Created about 8 years ago · Last pushed 11 months ago
Metadata Files
Readme Changelog Contributing License Code of conduct Support

README.Rmd

---
output:
  github_document:
    toc: true
    toc_depth: 3
    includes:
      before_body: header.md
---

```{r}
#| echo: false
options(width = 100)
suppressWarnings(suppressPackageStartupMessages(suppressMessages(library(dplyr))))
suppressWarnings(suppressMessages(library(pillar)))
```

## Installation

You can install the released version of ps from
[CRAN](https://CRAN.R-project.org) with:

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

If you need the development version, install it with

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

```{r}
library(ps)
library(pillar) # nicer printing of data frames
```

## 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.

```{r}
ps_pids()[1:20]
```

`ps()` returns a data frame, 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.

```{r}
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:

```{r}
p <- ps_handle()
p
```

### Query functions

`ps_pid(p)` returns the pid of the process.

```{r}
ps_pid(p)
```

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

```{r}
ps_create_time(p)
```

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.

```{r}
ps_is_running(p)
```

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

```{r}
ps_ppid(p)
```

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

```{r}
ps_parent(p)
```

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

```{r}
ps_name(p)
```

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

```{r}
ps_exe(p)
```

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

```{r}
ps_cmdline(p)
```

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

```{r}
ps_status(p)
```

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

```{r}
ps_username(p)
```

`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.

```{r}
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.

```{r}
ps_cwd(p)
```

`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_`.

```{r}
ps_terminal(p)
```

`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.

```{r}
ps_environ(p)[c("TERM", "USER", "SHELL", "R_HOME")]
```

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

```{r}
ps_num_threads(p)
```

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

```{r}
ps_cpu_times(p)
```

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

```{r}
ps_memory_info(p)
```

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

```{r}
ps_children(ps_parent(p))
```

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

```{r}
ps_num_fds(p)
f <- file(tmp <- tempfile(), "w")
ps_num_fds(p)
close(f)
unlink(tmp)
```

`ps_open_files(p)` lists all open files:

```{r}
ps_open_files(p)
f <- file(tmp <- tempfile(), "w")
ps_open_files(p)
close(f)
unlink(tmp)
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](http://psutil.readthedocs.io/en/latest/#recipes).

### Find process by name

Using `ps()` and dplyr:

```{r}
library(dplyr)
find_procs_by_name <- function(name) {
  ps() |>
    filter(name == !!name)  |>
    pull(ps_handle)
}

find_procs_by_name("R")
```

Without creating the full table of processes:

```{r}
find_procs_by_name <- function(name) {
  procs <- lapply(ps_pids(), function(p) {
    tryCatch({
      h <- ps_handle(p)
      if (ps_name(h) == name) h else NULL },
      no_such_process = function(e) NULL,
      access_denied = function(e) NULL
    )
  })
  procs[!vapply(procs, is.null, logical(1))]
  }

find_procs_by_name("R")
```

### Wait for a process to finish

`ps_wait()`, from ps 1.8.0, implements a new way, efficient for waiting on
a list of processes, so this is now very easy:

```{r}
px <- processx::process$new("sleep", "2")
p <- px$as_ps_handle()
ps_wait(p, 1000)
ps_wait(p)
```
### Wait for several processes to finish

Again, this is much simpler with `ps_wait()`, added in ps 1.8.0.

```{r}
px1 <- processx::process$new("sleep", "10")
px2 <- processx::process$new("sleep", "10")
px3 <- processx::process$new("sleep", "1")
px4 <- processx::process$new("sleep", "1")

p1 <- px1$as_ps_handle()
p2 <- px2$as_ps_handle()
p3 <- px3$as_ps_handle()
p4 <- px4$as_ps_handle()

ps_wait(list(p1, p2, p3, p4), timeout = 2000)
```

### Kill process tree

From ps 1.8.0, `ps_kill()` will first send `SIGTERM` signals on Unix,
and `SIGKILL` after a grace period, if needed.

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.

```{r}
kill_proc_tree <- function(pid, include_parent = TRUE, ...) {
  if (pid == Sys.getpid() && include_parent) stop("I refuse to kill myself")
  parent <- ps_handle(pid)
  children <- ps_children(parent, recursive = TRUE)
  if (include_parent) children <- c(children, list(parent))
  ps_kill(children, ...)
}

p1 <- processx::process$new("sleep", "10")
p2 <- processx::process$new("sleep", "10")
p3 <- processx::process$new("sleep", "10")
kill_proc_tree(Sys.getpid(), include_parent = FALSE)
```

### Filtering and sorting processes

Process name ending with "sh":

```{r}
ps() |>
  filter(grepl("sh$", name))
```

Processes owned by user:

```{r}
ps() |>
  filter(username == Sys.info()[["user"]]) |>
  select(pid, name)
```

Processes consuming more than 100MB of memory:

```{r}
ps() |>
  filter(rss > 100 * 1024 * 1024)
```

Top 3 memory consuming processes:

```{r}
ps() |>
  top_n(3, rss) |>
  arrange(desc(rss))
```

Top 3 processes which consumed the most CPU time:

```{r}
ps() |>
  mutate(cpu_time = user + system) |>
  top_n(3, cpu_time) |>
  arrange(desc(cpu_time)) |>
  select(pid, name, cpu_time)
```

## Code of Conduct

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

## License

MIT © RStudio

Owner

  • Name: R infrastructure
  • Login: r-lib
  • Kind: organization

GitHub Events

Total
  • Create event: 9
  • Release event: 3
  • Issues event: 22
  • Watch event: 4
  • Delete event: 2
  • Issue comment event: 31
  • Push event: 61
  • Pull request event: 24
  • Fork event: 2
Last Year
  • Create event: 9
  • Release event: 3
  • Issues event: 22
  • Watch event: 4
  • Delete event: 2
  • Issue comment event: 31
  • Push event: 61
  • Pull request event: 24
  • Fork event: 2

Committers

Last synced: 12 months ago

All Time
  • Total Commits: 741
  • Total Committers: 16
  • Avg Commits per committer: 46.313
  • Development Distribution Score (DDS): 0.07
Past Year
  • Commits: 140
  • Committers: 6
  • Avg Commits per committer: 23.333
  • Development Distribution Score (DDS): 0.1
Top Committers
Name Email Commits
Gábor Csárdi c****r@g****m 689
Michael Walshe m****e@a****k 15
Daniel Smith d****h@o****u 9
Elliott Sales de Andrade q****t@g****m 4
Oleh Khoma o****a@g****m 4
Sergey Fedorov b****a@m****g 4
Fran Barton f****n@a****t 3
Lionel Henry l****y@g****m 2
Mara Averick m****k@g****m 2
Michael Chirico c****m@g****m 2
Metin Yazici s****l@g****m 2
Brendan Furneaux b****x@g****m 1
George Stagg g****g@p****o 1
JFormoso j****o@g****m 1
reikoch 8****h 1
Florent Delmotte f****t@F****l 1
Committer Domains (Top 20 + Academic)

Issues and Pull Requests

Last synced: 10 months ago

All Time
  • Total issues: 90
  • Total pull requests: 83
  • Average time to close issues: 8 months
  • Average time to close pull requests: 3 months
  • Total issue authors: 36
  • Total pull request authors: 15
  • Average comments per issue: 2.64
  • Average comments per pull request: 0.83
  • Merged pull requests: 68
  • Bot issues: 0
  • Bot pull requests: 0
Past Year
  • Issues: 15
  • Pull requests: 33
  • Average time to close issues: 10 days
  • Average time to close pull requests: 10 days
  • Issue authors: 6
  • Pull request authors: 5
  • Average comments per issue: 1.87
  • Average comments per pull request: 0.7
  • Merged pull requests: 24
  • Bot issues: 0
  • Bot pull requests: 0
Top Authors
Issue Authors
  • gaborcsardi (47)
  • dansmith01 (4)
  • wlandau (3)
  • krlmlr (2)
  • michaelwalshe (2)
  • jeroen (2)
  • hongooi73 (1)
  • jp-luiggi (1)
  • jszhao (1)
  • rbreunev (1)
  • flodel (1)
  • francisbarton (1)
  • mike-molnar (1)
  • nevermana-landcare (1)
  • vikram-rawat (1)
Pull Request Authors
  • gaborcsardi (63)
  • dansmith01 (12)
  • lionel- (5)
  • barracuda156 (3)
  • MichaelChirico (3)
  • michaelwalshe (3)
  • JFormoso (2)
  • brendanf (2)
  • yurivict (2)
  • flodel (1)
  • QuLogic (1)
  • batpigandme (1)
  • francisbarton (1)
  • okhoma (1)
  • georgestagg (1)
Top Labels
Issue Labels
feature (21) bug (12) upkeep (9) tidy-dev-day :nerd_face: (3) documentation (2) help wanted :heart: (1) reprex (1)
Pull Request Labels
feature (2)

Packages

  • Total packages: 2
  • Total downloads:
    • cran 681,388 last-month
  • Total docker downloads: 86,409,183
  • Total dependent packages: 24
    (may contain duplicates)
  • Total dependent repositories: 172
    (may contain duplicates)
  • Total versions: 36
  • Total maintainers: 1
cran.r-project.org: ps

List, Query, Manipulate System Processes

  • Versions: 24
  • Dependent Packages: 18
  • Dependent Repositories: 112
  • Downloads: 681,388 Last month
  • Docker Downloads: 86,409,183
Rankings
Downloads: 0.2%
Dependent repos count: 2.0%
Dependent packages count: 3.8%
Forks count: 4.2%
Stargazers count: 5.2%
Average: 5.4%
Docker downloads count: 17.3%
Maintainers (1)
Last synced: 10 months ago
conda-forge.org: r-ps
  • Versions: 12
  • Dependent Packages: 6
  • Dependent Repositories: 60
Rankings
Dependent repos count: 4.5%
Dependent packages count: 9.0%
Average: 22.7%
Stargazers count: 36.5%
Forks count: 40.8%
Last synced: 10 months ago

Dependencies

DESCRIPTION cran
  • R >= 3.4 depends
  • utils * imports
  • R6 * suggests
  • callr * suggests
  • covr * suggests
  • curl * suggests
  • pillar * suggests
  • pingr * suggests
  • processx >= 3.1.0 suggests
  • rlang * suggests
  • testthat >= 3.0.0 suggests
.github/workflows/R-CMD-check.yaml actions
  • actions/checkout v2 composite
  • r-lib/actions/check-r-package v2 composite
  • r-lib/actions/setup-pandoc v2 composite
  • r-lib/actions/setup-r v2 composite
  • r-lib/actions/setup-r-dependencies v2 composite
.github/workflows/pkgdown.yaml actions
  • JamesIves/github-pages-deploy-action 4.1.4 composite
  • actions/checkout v2 composite
  • r-lib/actions/setup-pandoc v2 composite
  • r-lib/actions/setup-r v2 composite
  • r-lib/actions/setup-r-dependencies v2 composite
.github/workflows/pr-commands.yaml actions
  • actions/checkout v2 composite
  • r-lib/actions/pr-fetch v2 composite
  • r-lib/actions/pr-push v2 composite
  • r-lib/actions/setup-r v2 composite
  • r-lib/actions/setup-r-dependencies v2 composite
.github/workflows/test-coverage.yaml actions
  • actions/checkout v2 composite
  • r-lib/actions/setup-r v2 composite
  • r-lib/actions/setup-r-dependencies v2 composite
.github/workflows/rhub.yaml actions
  • actions/checkout v3 composite
  • r-hub/rhub2/actions/rhub-check v1 composite
  • r-hub/rhub2/actions/rhub-setup v1 composite
  • r-hub/rhub2/actions/rhub-setup-r v1 composite