Systemd service file for cardano-node

I use LiveView with systemd. The only drawback is, that you should not quit cardano-node while being attached to the session manually - the session will stall because there is no real tty connected to tmux. it will go crazy :wink: But other than that, it works quite well and systemd also restarts the thing if its killed/crashed. The trick to connect to the system tmux-session is to specify the tmux-socket path.

I added a system user called cardano:
sudo useradd -r -m -d /opt/cardano -s /sbin/nologin cardano

Permissions on home directory:
sudo chmod -R 770 /opt/cardano

Add your normal username to the group cardano:
sudo usermod -aG cardano *username*

Service file for systemd in /etc/systemd/system:

[Unit]
Description=Cardano Node
Requires=network.target

[Service]
Type=forking
Restart=always
RestartSec=5
User=cardano
Group=cardano
WorkingDirectory=/opt/cardano
ExecStart=/opt/cardano/service-start.sh
ExecStop=/opt/cardano/service-stop.sh
ExecReload=/opt/cardano/service-stop.sh && /opt/cardano/service-start.sh
LimitNOFILE=32768

[Install]
WantedBy=multi-user.target

I do have a subdirectory, named after the configuration in use for this node, e.g. mainnet-relay2, cotainging the topology, config, etc. Also in the home-directory /opt/cardano these scripts are present and executable.

service-start.sh:

#!/bin/bash
# Start cardano-node with settings from environment-file
source service-env.sh

CMD="cardano-node run \
  --topology ${CONFIG_HOME}/topology.json \
  --database-path ${CONFIG_HOME}/db \
  --socket-path ${CONFIG_HOME}/db/node.socket \
  --host-addr ${HOST_ADDRESS} \
  --port ${HOST_PORT} \
  --config ${CONFIG_HOME}/config.json"

# Check whether tmux session-name already exists
tmux -S ${TMUX_SOCKET} has-session -t "${TMUX_SESSION}" &> /dev/null

if [ $? -ne 0 ]; then
    # Start new session in tmux with socket path
    tmux -S ${TMUX_SOCKET} new-session -s ${TMUX_SESSION} -d ${CMD}
else
    # Message to stderr
    >&2 echo "Session \"${TMUX_SESSION}\" already exists on socket \"${TMUX_SOCKET}\"."
    exit 1
fi

service-stop.sh

#!/bin/bash

# Stop cardano-node with settings from environment-file
source service-env.sh

tmux -S ${TMUX_SOCKET} has-session -t "${TMUX_SESSION}" &> /dev/null

if [ $? -eq 0 ]; then
    echo -n "Killing session \"${TMUX_SESSION}\" on socket \"${TMUX_SOCKET}\"... "
    tmux -S ${TMUX_SOCKET} kill-session -t "${TMUX_SESSION}"
    if [ $? -eq 0 ]; then
        echo "Ok"
    else
        echo "Error"
        exit 1
    fi
else
    # Message to stderr
    >&2 echo "Session \"${TMUX_SESSION}\" not found on socket \"${TMUX_SOCKET}\"."
    exit 1
fi

service-env.sh:

#!/bin/bash

CONFIG_NAME="mainnet-relay2"
CONFIG_HOME="$(pwd)/${CONFIG_NAME}"
HOST_ADDRESS="YOUR_IP"
HOST_PORT=YOUR_PORT

# Specification of socket file allows users to share the sessions (if both in same group)
TMUX_SOCKET="${CONFIG_HOME}/tmux.socket"
# Session name
TMUX_SESSION="cardano_${CONFIG_NAME}"

service-attach.sh

#!/bin/bash

# Attach to tmux-session with settings from environment-file

source service-env.sh
tmux -S ${TMUX_SOCKET} attach-session -t "${TMUX_SESSION}"

Then, add the usual systemd reload, enable, and start. Be sure not to call the start script directly using sudo. Otherwise the service will not be able to overwrite the existing tmux.socket-file (owned by root) and cannot start at all.

When logging into your host using ssh, just execute

cd /opt/cardano/ && ./service-attach.sh

and you will be able to see the tmux-screen of the service executed as the system user cardano.

:slight_smile: