0

I'm trying to run tmux as a systemd user service ~/.config/systemd/user/tmux.service. When I start the service with systemd --user start tmux and then run tmux from a terminal, it seems to correctly connect to the tmux server of the service.

However, when I close the last session, the tmux service stops. This is a problem because I want the tmux server to stay available for SSH clients. How do I prevent the tmux server from automatically stopping after the last session is closed?

[Service]
Type=forking
ExecStart=tmux new-session -s %u -d
ExecStop=tmux kill-session -t %u

[Install]
WantedBy=default.target

Solution:

[Service]
Type=forking
ExecStart=tmux new -s dummy -d
ExecStop=-tmux kill-server
Restart=on-success

[Install]
WantedBy=default.target

2 Answers 2

3

tmux has an option exit-unattached which says If enabled, the server will exit when there are no attached clients, which you can disable with

set-option -s exit-unattached off

in your ~/.tmux.conf, but this doesn't seem to have any effect for me (on or off), or I have misunderstood what it means.

You can ask systemd to restart your service on exit by adding the line

Restart=on-success

You also need to edit the ExecStop to prefix the command with - in order to ignore the failure of the kill-session command when restarting (because the server is no longer running by then).

ExecStop=-/usr/bin/tmux kill-session -t %u
0

As of now, if you add this line to the .tmux.conf file, the server doesn't shut down when it has no sessions:

set -g exit-empty off

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .