rustdesk / rustdesk-server

RustDesk Server Program
https://rustdesk.com/server
GNU Affero General Public License v3.0
6.74k stars 1.45k forks source link

SysV Init Support #222

Open Ryushin opened 1 year ago

Ryushin commented 1 year ago

Please remove the systemd dependency for the deb packages and add sysvinit support. Those of us the run Debian with sysvinit (still supported) or Devuan can't use the packages.

Da-Geek commented 1 year ago

Just a Suggestion:- Another Option to run the RustDesk Server would be to use the containerised version as documented https://rustdesk.com/docs/en/self-host/install/#install-your-own-server-with-docker-compose

This would solve the dependency issue by not using the .deb at all.

Ryushin commented 1 year ago

The zip file has the binaries. I'll create init files and post them here. I guess all that would need to be done is to remove the systemd dependency from the deb package and include the sysvinit files in the package.

rustdesk commented 1 year ago

create init files

Go ahead, 👍

Ryushin commented 1 year ago

Details: Dowloaded the zip file with just the binaries. Extracted them in /opt/rustdesk. Created a local user account called rustdesk and changed the owner and group of /opt/rustdesk to the rustdesk user. Logs are stored in /var/log/rustdesk.

Init Scripts seem to work fine for start, stop, status, and restart.

hbbs SysV Init Script:

#! /bin/sh
### BEGIN INIT INFO
# Provides:          hbbs
# Required-Start:    $remote_fs $syslog $networking $local_fs
# Required-Stop:     $remote_fs $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: RustDesk Signal Server
# Description:       RustDesk Signal Server.
### END INIT INFO

# Author: Ryushin
#

# Do NOT "set -e"

PATH=/sbin:/usr/sbin:/bin:/usr/bin
DESC="RustDesk Signal Server"
NAME=hbbs
DAEMON=/opt/rustdesk/hbbs
DAEMON_ARGS=""
START_DIR="/opt/rustdesk"
PIDFILE=/var/run/$NAME.pid
DAEMON_USER=rustdesk
SCRIPTNAME=/etc/init.d/hbbs
LOG=/var/log/rustdesk/hbbs.log

# Read configuration variable file if it is present
[ -r /etc/default/$NAME ] && . /etc/default/$NAME

# Load the VERBOSE setting and other rcS variables
. /lib/init/vars.sh

# Define LSB log_* functions.
# Depend on lsb-base (>= 3.2-14) to ensure that this file is present
# and status_of_proc is working.
. /lib/lsb/init-functions

#
# Function that starts the daemon/service
#
do_start()
{
    echo "Starting $DESC... "
        start-stop-daemon --start --chuid $DAEMON_USER --user $DAEMON_USER --make-pidfile --background \
        --pidfile $PIDFILE --chdir $START_DIR --verbose --startas $DAEMON --no-close --output $LOG -- \
                $DAEMON_ARGS 
}

#
# Function that stops the daemon/service
#
do_stop()
{
        # Return
        #   0 if daemon has been stopped
        #   1 if daemon was already stopped
        #   2 if daemon could not be stopped
        #   other if a failure occurred
        start-stop-daemon --stop --verbose --retry=TERM/30/KILL/5 --pidfile $PIDFILE
        RETVAL="$?"
        [ "$RETVAL" = 2 ] && return 2
    echo
        # Many daemons don't delete their pidfiles when they exit.
        rm -f $PIDFILE
        return "$RETVAL"
}

case "$1" in
  start)
        do_start
        ;;
  stop)
        do_stop
        ;;
  status)
       status_of_proc -p "$PIDFILE" "$DAEMON" "$DAEMON_ARGS" && exit 0 || exit $?
       ;;
  restart)
        log_daemon_msg "Restarting $DESC" "$NAME"
    echo
        do_stop
        case "$?" in
          0|1)
                do_start
                case "$?" in
                        0) log_end_msg 0 ;;
                        1) log_end_msg 1 ;; # Old process is still running
                        *) log_end_msg 1 ;; # Failed to start
                esac
                ;;
          *)
                # Failed to stop
                log_end_msg 1
                ;;
        esac
        ;;
  *)
        echo "Usage: $SCRIPTNAME {start|stop|status|restart}" >&2
        exit 3
        ;;
esac

hbbr SysV Init Script:

#! /bin/sh
### BEGIN INIT INFO
# Provides:          hbbr
# Required-Start:    $remote_fs $syslog $networking $local_fs
# Required-Stop:     $remote_fs $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: RustDesk Relay Server
# Description:       RustDesk Relay Server.
### END INIT INFO

# Author: Ryushin
#

# Do NOT "set -e"

PATH=/sbin:/usr/sbin:/bin:/usr/bin
DESC="RustDesk Relay Server"
NAME=hbbr
DAEMON=/opt/rustdesk/hbbr
DAEMON_ARGS=""
START_DIR="/opt/rustdesk"
PIDFILE=/var/run/$NAME.pid
DAEMON_USER=rustdesk
SCRIPTNAME=/etc/init.d/hbbr
LOG=/var/log/rustdesk/hbbr.log

# Read configuration variable file if it is present
[ -r /etc/default/$NAME ] && . /etc/default/$NAME

# Load the VERBOSE setting and other rcS variables
. /lib/init/vars.sh

# Define LSB log_* functions.
# Depend on lsb-base (>= 3.2-14) to ensure that this file is present
# and status_of_proc is working.
. /lib/lsb/init-functions

#
# Function that starts the daemon/service
#
do_start()
{
    echo "Starting $DESC... "
        start-stop-daemon --start --chuid $DAEMON_USER --user $DAEMON_USER --make-pidfile --background \
        --pidfile $PIDFILE --chdir $START_DIR --verbose --startas $DAEMON --output $LOG -- \
                $DAEMON_ARGS 
}

#
# Function that stops the daemon/service
#
do_stop()
{
        # Return
        #   0 if daemon has been stopped
        #   1 if daemon was already stopped
        #   2 if daemon could not be stopped
        #   other if a failure occurred
        start-stop-daemon --stop --verbose --retry=TERM/30/KILL/5 --pidfile $PIDFILE
        RETVAL="$?"
        [ "$RETVAL" = 2 ] && return 2
    echo
        # Many daemons don't delete their pidfiles when they exit.
        rm -f $PIDFILE
        return "$RETVAL"
}

case "$1" in
  start)
        do_start
        ;;
  stop)
        do_stop
        ;;
  status)
       status_of_proc -p "$PIDFILE" "$DAEMON" "$DAEMON_ARGS" && exit 0 || exit $?
       ;;
  restart)
        log_daemon_msg "Restarting $DESC" "$NAME"
    echo
        do_stop
        case "$?" in
          0|1)
                do_start
                case "$?" in
                        0) log_end_msg 0 ;;
                        1) log_end_msg 1 ;; # Old process is still running
                        *) log_end_msg 1 ;; # Failed to start
                esac
                ;;
          *)
                # Failed to stop
                log_end_msg 1
                ;;
        esac
        ;;
  *)
        echo "Usage: $SCRIPTNAME {start|stop|status|restart}" >&2
        exit 3/var/log/rustdesk/*.log {
    daily
    missingok
    rotate 14
    notifempty
    create 640 rustdesk rustdesk
    sharedscripts
    prerotate
    /etc/init.d/hbbr stop
    /etc/init.d/hbbs stop
    endscript
    postrotate
    /etc/init.d/hbbs start
    /etc/init.d/hbbr start
    endscript
}
        ;;
esac

Log Rotate config. To be place in /etc/logrotate.d/:

/var/log/rustdesk/*.log {
    daily
    missingok
    rotate 14
    notifempty
    create 640 rustdesk rustdesk
    sharedscripts
    prerotate
    /etc/init.d/hbbr stop
    /etc/init.d/hbbs stop
    endscript
    postrotate
    /etc/init.d/hbbs start
    /etc/init.d/hbbr start
    endscript
}

HBBS and HBBR daemon improvement requests: Add the --daemon option so program can fork into the background. Add the --pid option so the program can create a PID at a location of the background process. Add the --log option to send the daemon's logs to a specific location.