Tmux with Systemd
You can start tmux sessions automatically at boot using systemd user services. This is useful for servers that need persistent workspaces available immediately after startup.
Use Case
Automatically launch a monitoring dashboard or application session when the server starts, even before any user logs in.
Systemd User Service for Tmux
Create a systemd user service:
mkdir -p ~/.config/systemd/user/
~/.config/systemd/user/tmux-work.service
[Unit]
Description=Tmux work session
After=default.target
[Service]
Type=forking
ExecStart=/usr/bin/tmux new-session -d -s work
ExecStop=/usr/bin/tmux kill-session -t work
Restart=on-failure
KillMode=none
[Install]
WantedBy=default.target
Enable and start:
# Enable to start at login
systemctl --user enable tmux-work.service
# Start now
systemctl --user start tmux-work.service
# Check status
systemctl --user status tmux-work.service
Session with Full Layout via Service
Point the service to a setup script:
~/.config/systemd/user/tmux-monitor.service
[Unit]
Description=Tmux monitoring session
After=network.target
[Service]
Type=forking
ExecStart=/home/YOUR_USER/bin/monitor.sh
ExecStop=/usr/bin/tmux kill-session -t monitoring
Restart=on-failure
KillMode=none
[Install]
WantedBy=default.target
The monitor.sh script starts the session detached (as shown in the scripting module).
System-Wide Service (Root)
For system services (not per-user), create in /etc/systemd/system/:
/etc/systemd/system/tmux-appmonitor.service
[Unit]
Description=Application Monitor in Tmux
After=network.target
[Service]
Type=forking
User=appuser
ExecStart=/usr/bin/tmux new-session -d -s monitor
ExecStop=/usr/bin/tmux kill-session -t monitor
Restart=on-failure
KillMode=none
[Install]
WantedBy=multi-user.target
sudo systemctl daemon-reload
sudo systemctl enable tmux-appmonitor.service
sudo systemctl start tmux-appmonitor.service
Enabling User Lingering (Start at Boot Before Login)
By default, user systemd services only run while logged in. To run at boot:
sudo loginctl enable-linger YOUR_USER
This keeps user services alive from boot, even without an active login session.
Checking and Debugging
# Check user service logs
journalctl --user -u tmux-work.service -f
# Verify tmux session is running
tmux ls
# Manual start for testing
systemctl --user start tmux-work.service