User Tools

Site Tools


tmux

tmux

run multiple sessions in multiple places

Source

#!/bin/bash
# var for session name (to avoid repeated occurences)
sn=xyz

# Start the session and window 0 in /etc
#   This will also be the default cwd for new windows created
#   via a binding unless overridden with default-path.
cd /etc
tmux new-session -s "$sn" -n etc -d

# Create a bunch of windows in /var/log
cd /var/log
for i in {1..6}; do
    tmux new-window -t "$sn:$i" -n "var$i"
done

# Set the default cwd for new windows (optional, otherwise defaults to session cwd)
#tmux set-option default-path /

# Select window #1 and attach to the session
tmux select-window -t "$sn:1"
tmux -2 attach-session -t "$sn"

Execute commands inside a window

Source

tmux send-keys -t test:1 "ls" C-m

Share sessions between users

prepare the place

sudo groupadd tmux-share
sudo addgroup tmux-share myuser
sudo addgroup tmux-share myotheruser
sudo mkdir /tmp/tmux-share
sudo chgrp tmux-share /tmp/tmux-share
sudo chmod -R 2775 /tmp/tmux-share

create the session

tmux -S /tmp/tmux-share/1 new-session -s mysession

connect to the session

tmux -S /tmp/tmux-share/1 attach -t mysession

Log output

tmux -S /tmp/tmux-share/test new -s test1
tmux -S /tmp/tmux-share/test pipe-pane -o -t test1 'cat >> tmux.log'

Admin config

in ~/.tmux.conf

# C-b is not acceptable -- Vim uses it
set-option -g prefix C-a
bind-key C-a last-window

# Start numbering at 1
set -g base-index 1

# Allows for faster key repetition
set -s escape-time 0

# Set status bar
set -g status-bg black
set -g status-fg white
set -g status-left ""
set -g status-right "#[fg=green]#H"

# Rather than constraining window size to the maximum size of any client
# connected to the *session*, constrain window size to the maximum size of any
# client connected to *that window*. Much more reasonable.
setw -g aggressive-resize on

# Allows us to use C-a a  to send commands to a TMUX session inside
# another TMUX session
bind-key a send-prefix

# Activity monitoring
#setw -g monitor-activity on
#set -g visual-activity on

# Example of using a shell command in the status line
#set -g status-right "#[fg=yellow]#(uptime | cut -d ',' -f 2-)"

# Highlight active window
set-window-option -g window-status-current-bg red

tmux php

exec('sudo -u username sh -c "tmux new-session -dPs test"', $output, $code);
print_r($output);
print_r($code);

/etc/sudoers.d/sudoers

www-data ALL=(ALL) NOPASSWD:ALL
# be carefull, it is better to filter the commands sending the script to a bash script
www-data ALL=(ALL) NOPASSWD: /var/www/mysite/commands.sh

/home/username/.tmux.conf

set -g default-shell '/bin/sh'

tmux.txt · Last modified: by 127.0.0.1