Linux OS restart via systemd

XorMon application control can be converted into a systemd service.

The advantage of this solution is that the ability to bind ports below 1024 is granted only to XorMon, not globally to the Node.js binary.

XorMon can still be controlled using the 'xormon.sh' script. The script recognizes that Xormon is running as a service and delegates control to systemd.

Similarly, other actions that require the application to be restarted, such as an upgrade or the installation of an SSL certificate, are automatically handled by systemd.

Prerequisites

  • XorMon version 2.4.14
  • The user "xormon" must have permission to control the Xormon service using sudo

XorMon service controls

The user xormon calls the service using sudo:
sudo systemctl start/restart/status/stop xormon.service

Setup instructions

  • Stop XorMon (as xormon)
    cd ~/xormon-ng; ./xormon.sh stop
  • Create new sudoers file for user xormon (as root)
    Copy/paste and run as a single command:
    su -
    cat << EOF > ~/xormon.sudo
    # XorMon: lets user xormon control its systemd service.
    # start/status/restart/stop: xormon.sh start|restart|stop run from a terminal (start maps to restart)
    # --no-block restart: called by xormon.sh from inside the service (web UI restart, upgrade)
    # start: optional, only for running "sudo systemctl start xormon.service" directly
    xormon ALL=(root) NOPASSWD: /usr/bin/systemctl start xormon.service, \
                                /usr/bin/systemctl restart xormon.service, \
                                /usr/bin/systemctl stop xormon.service, \
                                /usr/bin/systemctl status xormon.service, \
                                /usr/bin/systemctl --no-block restart xormon.service
    EOF
    Install the file
    visudo -cf ~/xormon.sudo && install -m 0440 -o root -g root ~/xormon.sudo /etc/sudoers.d/xormon
  • Create XorMon service (as root)
    Copy/paste and run as a single command:
    cat << EOF > /etc/systemd/system/xormon.service
    > [Unit]
    Description=XorMon NG infrastructure monitoring
    Documentation=https://xormon.com
    Wants=network-online.target
    After=network-online.target postgresql.service
    StartLimitIntervalSec=600
    # Counts every start incl. restarts requested by xormon.sh (terminal, web UI, upgrade), stops crash loops only
    StartLimitBurst=10
    
    [Service]
    # xormon.sh start daemonizes the process manager and exits, the manager writes its PID to the lock file.
    # Every start/restart/stop (terminal, web UI, upgrade) is passed by xormon.sh to systemd, so the main PID
    # changes only when systemd itself restarts the service.
    Type=forking
    PIDFile=/home/xormon/.xormon/manager.lock
    User=xormon
    Group=xormon
    WorkingDirectory=/home/xormon/xormon-ng
    Environment=LANG=en_US.UTF-8
    ExecStart=/home/xormon/xormon-ng/xormon.sh start
    ExecStop=/home/xormon/xormon-ng/xormon.sh stop
    
    # Allow binding to privileged ports (443) for this service only, no file capability on node needed.
    # No CapabilityBoundingSet: xormon.sh calls sudo systemctl from inside the service (web UI restart, upgrade).
    AmbientCapabilities=CAP_NET_BIND_SERVICE
    
    LimitNOFILE=32768
    LimitNPROC=64000
    
    Restart=on-failure
    RestartSec=10
    TimeoutStartSec=300
    TimeoutStopSec=60
    
    [Install]
    WantedBy=multi-user.target
    > EOF
    Set file permissions
    chomd 644 /etc/systemd/system/xormon.service
    Reload services
    systemctl daemon-reload
  • Enable XorMon service (as root)
    Don't skip this step.
    It's important for the application to work properly and ensures that XorMon starts automatically at boot time.
    systemctl enable xormon.service
  • Start XorMon (as xormon)
    sudo systemctl start xormon.service

Dernière mise à jour :