39 lines
1.8 KiB
Bash
39 lines
1.8 KiB
Bash
#!/bin/bash
|
|
|
|
# Lacking an existing tmux session, create one with a number of running windows.
|
|
# I want to run tmux for root in one screen window, and tmux for current user in another screen window;
|
|
# take the session name as a parameter
|
|
|
|
# start detached (-d), with session name (-s), then new windows with title (-t) per command to run
|
|
# #$user_id can be either the id of the current user, or #0 when called for root
|
|
create_tmux_session () {
|
|
local session_name=$1
|
|
local user_id = $2
|
|
sudo -u "#$user_id" tmux new-session -d -s $session_name
|
|
sudo -u "#$user_id" tmux new-window -t $session_name:1 -n 'htop' 'htop'
|
|
sudo -u "#$user_id" tmux new-window -t $session_name:1 -n 'iftop' 'iftop'
|
|
sudo -u "#$user_id" tmux new-window -t $session_name:1 -n 'powertop' 'powertop'
|
|
sudo -u "#$user_id" tmux new-window -t $session_name:1 -n 'logs' 'cd /var/log'
|
|
sudo -u "#$user_id" tmux new-window -t $session_name:1 -n 'commands'
|
|
}
|
|
|
|
# before creating a new session, check if it exists. If so, attach to it
|
|
# use `sudo -u #` to allow for easy switching between user and root session in calling
|
|
attach_or_create_tmux_session () {
|
|
local session_name=$1
|
|
if tmux has-session -t $session_name 2>/dev/null; then
|
|
echo "Sessie $session_name gevonden, attaching..."
|
|
sudo -u "#$user_id" tmux attach-session -t $session_name
|
|
else
|
|
echo "Sessie $session_name niet gevonden, create & attach..."
|
|
attach_or_create_tmux_session $session_name $user_id
|
|
sudo -u "#$user_id" tmux attach-session -t $session_name
|
|
}
|
|
|
|
# start/attach a session for the current user
|
|
current_user=$(whoami)
|
|
current_user_id=$(id -u)
|
|
attach_or_create_tmux_session "$current_user" "$current_user_id"
|
|
|
|
# start/attach a session for root
|
|
attach_or_create_tmux_session "root" "0"
|