Skip to content

Instantly share code, notes, and snippets.

@benjaminguinaudeau
Created November 13, 2020 00:57
Show Gist options
  • Save benjaminguinaudeau/4b4750fbbf8edceabedf80a6a1f6a584 to your computer and use it in GitHub Desktop.
Save benjaminguinaudeau/4b4750fbbf8edceabedf80a6a1f6a584 to your computer and use it in GitHub Desktop.
Retrieve current running process on linux/mac os
get_proc <- function(){
proc <- system("ps -aux", intern = T)
proc[1] %>% str_extract(".{12}")
end_first_column <- proc %>%
map_dbl(~str_length(str_extract(.x, "[^\\s]+"))) %>%
max
col_names <- proc[1] %>%
stringr::str_extract_all("(?<=\\s{1}|^).*?(\\s{1,}|$)") %>%
.[[1]] %>%
stringr::str_trim(.)
border <- proc[1] %>%
# stringr::str_locate_all("(?<=\\s{1}|^).*?(\\s{1,}|$)") %>%
stringr::str_locate_all("(\\s{1,}|^).*?(?=\\s{1}|$)") %>%
.[[1]]
border[1,2] <- end_first_column
border[2,1] <- end_first_column + 1
proc %>%
tail(-1) %>%
purrr::map_dfr(~{
border[nrow(border), 2] <- stringr::str_length(.x)
.x %>%
stringr::str_sub(start = border[, 1], end = border[, 2]) %>% t %>%
tibble::as_tibble(.) %>%
purrr::set_names(col_names)
}) %>% janitor::clean_names(.)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment