Search code examples
linuxsystemdtmuxsigterm

Preventing tmux session created by systemd from automatically terminating on Ctrl+C


Since a few days I'm successfully running the new Minecraft Bedrock Edition dedicated server on my Ubuntu 18.04 LTS home server. Because it should be available 24/7 and automatically startup after boot I created a systemd service for a detached tmux session:

tmux.minecraftserver.service

[Unit]
Description=tmux minecraft_server detached

[Service]
Type=forking
WorkingDirectory=/home/mine/minecraftserver
ExecStart=/usr/bin/tmux new -s minecraftserver -d "LD_LIBRARY_PATH=. /home/mine/minecraftser$
User=mine

[Install]
WantedBy=multi-user.target

Everything works as expected but there's one tiny thing that keeps bugging me:

How can I prevent tmux from terminating it's whole session when I press Ctrl+C ? I just want to terminate the Minecraft server process itself instead of the whole tmux session. When starting the server from the command line in a manually created tmux session this does work (session stays alive) but not when the session was brought up by systemd.


Solution

  • I as able to solve this by using systemd's ExecStartPost and tmux's send-keys like this:

    [Unit]
    Description=tmux minecraft_server detached
    
    [Service]
    Type=forking
    WorkingDirectory=/home/mine/minecraftserver
    ExecStart=/usr/bin/tmux new -d -s minecraftserver
    ExecStartPost=/usr/bin/tmux send-keys -t minecraftserver "cd /home/mine/minecraftserver/" Enter "LD_LIBRARY_PATH=. ./bedrock_server" Enter
    
    User=mine
    
    [Install]
    WantedBy=multi-user.target