User:Trickygnome/Tmux cleaning
From Gentoo Wiki
Jump to:navigation
Jump to:search
When tmux used with many terminals a question arises to clean empty opened ones.
Sway - foot
FILE
/home/larry/.bash_aliases
_check_to_kill() {
# ps axjf
# 1 28168 28167 28167 ? -1 Sl 1000 0:03 foot
# 28168 28169 28169 28169 pts/11 28169 Ss+ 1000 0:00 \_ /bin/bash
# 28169 28202 28169 28169 pts/11 28169 S+ 1000 0:00 \_ tmux new-session
# - check children count
local child_count=$(ps -eo ppid | grep -cx "$1")
if [[ $child_count -eq 0 ]]; then
echo "child_count=$child_count -eq 0" pid=$1
echo kill -s 9 $1
kill -s 9 $1
elif [[ $child_count -gt 1 ]]; then
echo "child_count -gt 1"
return
fi
}
_check_to_kill_foot() {
# ps axjf
# 1 28168 28167 28167 ? -1 Sl 1000 0:03 foot
# 28168 28169 28169 28169 pts/11 28169 Ss+ 1000 0:00 \_ /bin/bash
# 28169 28202 28169 28169 pts/11 28169 S+ 1000 0:00 \_ tmux new-session
# - check children count
pp=$1
local child_count=$(ps -eo ppid | grep -cx "$1")
if [[ $child_count -eq 0 ]]; then
echo "child_count=$child_count -eq 0" pid=$1
echo "kill -s 9 $1"
kill -s 9 $1
elif [[ $child_count -gt 1 ]]; then
echo "child_count -gt 1"
return
fi
# - one child - check child name
local line=$(ps -o pid,command --ppid "$1" --no-headers)
local pid=$(echo "$line" | awk '{print $1}')
local command=$(echo "$line" | awk '{$1=""; sub(/^[[:space:]]+/, ""); print}')
echo $pid $command
if [[ "$command" == '/bin/bash' || "$command" == 'bash' ]]; then
echo "command" == '/bin/bash'
# - check children count
local child_count=$(ps -eo ppid | grep -cx "$pid")
if [[ $child_count -eq 0 ]]; then
echo "2) child_count -eq 0"
echo "2) kill -s 9 $1"
kill -s 9 $pp
elif [[ $child_count -gt 1 ]]; then
echo "2) child_count -gt 1"
return
fi
fi
}
cl() { # sway+tmux cleaning
children=
parent_pid=$(ps -eo pid,stat,command --no-headers | grep 'tmux new-session$' | grep 'Ss' | awk '{print $1}')
mapfile -t children < <(ps -o pid --ppid "$parent_pid" --no-headers)
for child_pid in "${children[@]}"; do
_check_to_kill "$child_pid"
done
# - kill lonely "-bash"
children=
mapfile -t children < <(ps axjf | grep 'foot' | grep 'Sl' | awk '{print $2}')
echo "${children[@]}"
for child_pid in "${children[@]}"; do
_check_to_kill_foot "$child_pid"
done
}
Xfce4 - xfce4-terminal
FILE
/home/larry/.bash_aliases
cl() {
ps aux | grep '\-bash' | grep Ss+ | tr -s ' ' | cut -f 2 -d ' ' | xargs kill -s 9 &>/dev/null
ps aux | grep "tmux new-session bash -c 'ssh" | tr -s ' ' | cut -f 2 -d ' ' | xargs kill -s 9 &>/dev/null
}