How do I start a tmux service in the user's directory on user login using systemd

Viewed 1157

I'm trying to set up my system so that when a user logs in, a tmux session will automatically be created for them, and this session is restarted if it ever exits, and the session starts in the user's home directory. I would like this to work for any user, or any new user added to the system, without a static unit file for each user. I'm having trouble making this work in a generic way, because I need to specify User and WorkingDirectory in the unit file for the tmux session to be created for the correct user in the correct directory.

So far my unit file looks like the following:

/etc/systemd/system/tmux-session-service.service...
---------------------------------------------------

[Unit]
Description=Tmux Session Service

[Service]
Type=forking
User=my-user
WorkingDirectory=/home/my-user
ExecStart=/usr/bin/tmux new-session -s tmux-session-service -d
ExecStop=/usr/bin/tmux kill-session -t tmux-session-service
Restart=on-failure

[Install]
WantedBy=multi-user.target

When I install and enable this, everything works like I expect as long as I am logged is as my-user. However if I log in as another user, the tmux session isn't created with the right permissions or working directory for the new user.

I looked into template files, but I can't quite get things to work. I tried setting the target to default.target, and using the %u template directive, but that seems to just refer to the user running the service manager, which is root.

One option would be to run systemctl run tmux-session-service@new-user.service when new-user logs in. Then I could use %i in the User and WorkingDirectory directives in the unit file. But then I need some process that has systemctl permissions to kick that off on user login, and I can't think of a way to do that.

I'm running:

  • Arch Linux
  • tmux v3.1c
  • systemd v247
2 Answers

Under Ubuntu, I install the ....service file under:

/usr/lib/systemd/user/...

With the Debian packager, that is not automatic if you installed using the .service in the debian installation folder. Instead, you have to do it manually so it goes in the correct folder. So say you have a project defined like so:

tmux-session-service/debian/tmux-session-service.docs
tmux-session-service/debian/tmux-session-service.install
tmux-session-service/debian/tmux-session-service.service   <-- wrong!   

Then for each user I would enable the service like so:

# Make sure the target folder exists
mkdir -p /home/${USER}/.config/systemd/user/default.target.wants

# If you're root when doing that, you want to fix the ownership
# (for the group, you may need a different variable)
chown -R ${USER}:${USER} /home/${USER}/.config/systemd

# If already installed, remove the link before re-creating it
rm -f /home/${USER}/.config/systemd/user/default.target.wants/${SERVICE}.service

# Again, I do this as root, so I need to use special care to run the
# following command as $USER isntead
sudo -H -u ${USER} sh -c "ln -s /usr/lib/systemd/user/${SERVICE}.service /home/${USER}/.config/systemd/user/default.target.wants/${SERVICE}.service"

If you want (can) do it manually, then install the file under /usr/lib/systemd/user/... as mentioned above, and then use the enable option:

systemctl --user enable ${SERVICE}.service

The problem with this technique is that you need to log in as each user to order to enable your service.

I think there is a way to have a service auto-start for all users, but that I haven't found out how to make it work yet...

Could it be as simple as putting something in new user's .bashrc files (or in the /etc/profile file to get a system wide effect) that attaches to a tmux session called 'main' (this would create it if it doesn't already exist)?

That's what I have in mine, and it's as if tmux is just a built in feature of my terminal:

# Launch tmux
if command -v tmux>/dev/null; then
    [[ ! $TERM =~ screen ]] && [ -z $TMUX ] && tmux new-session -A -s main
fi
Related