Created
January 30, 2024 22:39
-
-
Save thunderpoot/4b0ef4e2dd5c7b3fce91af23d509f5c4 to your computer and use it in GitHub Desktop.
Mosh: You have N detached Mosh sessions on this server
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# You know that really annoying message that pops up... | |
# Mosh: You have 3 detached Mosh sessions on this server, with PIDs: | |
# - mosh [2294539] | |
# - mosh [1874313] | |
# - mosh [2294805] | |
# I often find myself copying this list of PIDs in order to kill them manually | |
# ... so this script was born. Maybe others will find it useful. | |
declare -A colours | |
colours[green]="\e[32;1m" | |
colours[blue]="\e[34;1m" | |
colours[red]="\e[31;1m" | |
colours[reset]="\e[0m" | |
printc() | |
{ | |
local colour=${colours[$1]} message=$2 | |
printf "${colour}${message}${colours[reset]}" | |
} | |
printf "Please paste the list of detached Mosh sessions (^D ends input)\n" | |
printf "===============================================================\n" | |
# Read multi-line input from user | |
input_list=$(cat) | |
# Extract PIDs from the pasted list | |
pids=$(echo "$input_list" | grep -oP '\[\K[0-9]+') | |
# Check if any PIDs were found | |
if [ -z "$pids" ]; then | |
printc blue "No PIDs found in the input.\n" | |
else | |
# Confirm with the user before killing the sessions | |
printc blue "The following PIDs will be terminated:\n" | |
echo "$pids" | awk '{print " - "$1}' | |
printc blue "Do you want to proceed? [y/N] " | |
read confirmation | |
if [[ "$confirmation" =~ ^[yY](es)?|YES$ ]]; then | |
# Kill each PID | |
for pid in $pids; do | |
printc blue "Killing Mosh session with PID $pid\n" | |
kill $pid | |
done | |
printc green "Done.\n" | |
else | |
printc red "Operation cancelled.\n" | |
fi | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment