Skip to content

Instantly share code, notes, and snippets.

@evanlinde
Created October 7, 2019 17:41
Show Gist options
  • Save evanlinde/ddaf6ab649dee7c7f9c8bbb9630a11fe to your computer and use it in GitHub Desktop.
Save evanlinde/ddaf6ab649dee7c7f9c8bbb9630a11fe to your computer and use it in GitHub Desktop.
Show running RStudio Server sessions
#!/bin/bash
#
# Print basic info about running RStudio Server sessions
#
# Requires root/sudo for accurate output.
# Does not include info about suspended sessions (i.e. where no rsession
# process is currently running).
# Start time is when the rsession process started; if session was previously
# suspended and relaunched, this will be the the relaunch time.
#
RED=$(tput setaf 1; tput bold) #"\033[1;31m"
GREEN=$(tput setaf 2; tput bold) #"\033[1;32m"
YELLOW=$(tput setaf 3; tput bold) #"\033[1;33m"
ENDCOLOR=$(tput sgr0) #"\033[0m"
# Format string for printf
_printf="%7s %-10s %-19s %-12s\n"
old_date=$(date -d "-30 days" +%s)
printf "\n${_printf}" PID USERNAME START_TIME STATUS
active_pids=($(ss -xp -o state established | sed -nE '/\"rsession\"/{s/^.*.pid\=([0-9]+),.*$/\1/;p}'))
ps h -C rsession -o user,pid,cmd | grep 'rstudio-server' | while read username pid cmd; do
start_time_s=$(date -r /proc/${pid} +%s);
start_time=$(printf "%(%Y-%m-%d %H:%M:%S)T" ${start_time_s})
[[ ${start_time_s} -lt ${old_date} ]] && start_time="${YELLOW}${start_time}${ENDCOLOR}";
[[ " ${active_pids[@]} " =~ " ${pid} " ]] && status="${GREEN}active${ENDCOLOR}" || status="${RED}disconnected${ENDCOLOR}";
printf "${_printf}" ${pid} ${username} "${start_time}" "${status}";
done
echo ""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment