Site icon R-bloggers

Kill all R sessions

[This article was first published on Teoten's blog, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
< article id="post-/posts/2024/kill_all_r_sessions/" data-post-id="/posts/2024/kill_all_r_sessions/">

When working in R, there are times when managing multiple R sessions becomes essential. For instance, if you are updating packages, troubleshooting conflicts, or managing system resources, it may be necessary to terminate all the sessions, or to keep only the current R session.

In this blog post, we’ll walk through a simple R function that achieves this using only base R functions, ensuring no additional dependencies are introduced.

Kill all R sessions

kill_other_R_sessions <- function() { 
  current_PID <- Sys.getpid()
  os <- Sys.info()['sysname']

  if (os == "Linux") {
    progs <- system("ps aux", intern = TRUE)
    Rsessions <- progs[grep("R/bin/exec", progs)]
  } else if (os == "Windows") {
    progs <- system("tasklist", intern = TRUE)
    Rsessions <- progs[grep("^R\\.exe|^Rterm\\.exe|^Rscript", progs)]
  } else {
    stop("System not supported.")
  }
  
  current_sessions <- strsplit(Rsessions, "[[:space:]]") |> 
    lapply(function(x) ifelse(x == "", NA, x)) |> 
    lapply(na.exclude) |> 
    lapply(as.vector) |> 
    sapply(`[`, 2)
  
  kill_sessions <- current_sessions[current_sessions != current_PID]

  if (os == "Linux") {
    for(PID in kill_sessions) system(paste0("kill ", PID)) 
  } else if (os == "Windows") {
    for(PID in kill_sessions) shell(paste0("taskkill /F /PID ", PID))
  } else {
    stop("System not supported.")
  }
}

How the Function Works

  • Step 1: Identify the Current Process. The function starts by getting the process ID (PID) of the current R session using `Sys.getpid()`. This PID is stored in the current_PID variable, which will be excluded from termination.

  • Step 2: Determine the Operating System. Next, we use `Sys.info()[‘sysname’]` to determine whether the system is Linux or Windows. This allows us to tailor the commands for each platform.

  • Step 3: Retrieve All R Sessions. The `system()` function executes platform-specific commands to list all running processes.

  • On Linux, `ps aux` is used, and we filter R processes by searching for “R/bin/exec”.

  • On Windows, we use `tasklist`, and the function searches for “R.exe”, “Rterm.exe”, or “Rscript”.

  • Step 4: Parse the Process List. The process output is split and cleaned using base R’s `strsplit()` and `lapply()` to extract the relevant PIDs. The list of PIDs is then checked against current_PID to ensure that we do not kill the running session.

  • Why Base R Only?

    In this function, we use only base R functions like `system()`, `grep()`, and `lapply()` to avoid external dependencies. This is crucial in scenarios like package updates or system maintenance where introducing new dependencies could cause conflicts. Using base R ensures that the function remains lightweight, compatible, and reliable across various environments.

    Use Cases

    Limitations

    Cross-Platform Considerations

    This function only supports Linux and Windows environments. MacOS is not explicitly covered, but extending support could be done by modifying the function to handle macOS-specific process management commands. Also, some particular Linux distributions might produce a different result when calling `ps`.

    Permissions

    Depending on the system’s configuration, users may need appropriate permissions to kill processes, especially when R sessions were started by other users.

    Conclusion

    This simple function leverages only base R to kill other R sessions and can be a handy tool for managing resources, avoiding package conflicts, and debugging. By keeping dependencies minimal, it ensures stability and broad compatibility. You can easily adapt it for your own workflows and make sure that your R environment stays tidy and efficient.

    Feel free to copy and customize this function to suit your needs!

    To leave a comment for the author, please follow the link and comment on their blog: Teoten's blog.

    R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
    Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
    Exit mobile version