使用带有 start-stop-daemon、sysv 的屏幕

使用带有 start-stop-daemon、sysv 的屏幕

我正在尝试使用 screen 运行一个带有 ncurse 接口的程序作为守护进程。我想使用 start-stop-daemon 来管理进程,但在创建 SysV init 脚本时遇到了麻烦。

变量:

NAME=rtorrent
CHDIR=/opt/$NAME
DAEMON=$NAME
DAEMON_ARGS="-d -m -S $NAME $DAEMON &> /dev/null"
USER=media
GROUP=media
PIDFILE=/var/run/$NAME.pid

目前,我的启动功能是这样的:

do_start()
{
    # Return
    #   0 if daemon has been started
    #   1 if daemon was already running
    #   2 if daemon could not be started
    pgrep -F $PIDFILE > /dev/null 2>&1 || return 1
    start-stop-daemon --start --quiet --make-pidfile --pidfile $PIDFILE --chuid $USER:$GROUP --chdir $CHDIR --background --exec screen -- $DAEMON_ARGS || return 2
}

但这存储的是sleep进程 ID。可以想象,守护进程可能会关闭,而sleep进程仍在运行

我的停止功能需要停止两者,因此我有:

do_stop()
{
    # Return
    #   0 if daemon has been stopped
    #   1 if daemon was already stopped
    #   2 if daemon could not be stopped
    pgrep -F $PIDFILE > /dev/null 2>&1 || return 1
    for i in `ps -C $NAME -o pid=` ; do kill $i ; done
    pgrep -F $PIDFILE > /dev/null 2>&1 || return 2

    # Many daemons don't delete their pidfiles when they exit.
    rm -f $PIDFILE
}

它应该可以工作(尚未测试),但问题是它会关闭任何其他用户以其$NAME名称或参数创建的任何其他进程。

我认为我的解决方案是让我的函数仅返回由 创建的ps进程名称 的 pid 。因为此守护进程将在专用用户 ID 下运行。$NAME$USER

我不知道如何获得此输出。ps -C $NAME -u $USER o pid=为每个匹配项提供一个列表,但我想要两个匹配项都有一个列表。以防我决定此用户可以稍后处理其他进程。

另外,重新加载怎么样? start-stop-daemon --stop --signal 1 --quiet --pidfile $PIDFILE --name $NAME会重新加载screen进程吗?会重新加载守护进程吗?有没有更好的方法?任何帮助都非常感谢。

答案1

我需要相同的 Minecraft Bedrock 服务器作为守护进程,但我的duckduckgofu无法找到解决方案……最终我组装了以下初始化脚本。

需要注意的关键点是,它start-stop-deamon不用于启动屏幕会话,我们screen守护进程(分离)模式。

#!/bin/sh
### BEGIN INIT INFO
# Provides: PocketMine-MP
# Required-Start: $network $remote_fs $local_fs
# Required-Stop: $network $remote_fs $local_fs
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: PocketMine-MP (Minecraft Bedrock/PE).
# Description: PocketMine-MP (Minecraft Bedrock/PE) Server for my children and tribe.
### END INIT INFO

# Auhtor: Daniel Sokolowski <[email protected]>

# Install by typing update-rc.d <FILEsNAME> defaults

sPATH=/sbin:/usr/sbin:/bin:/usr/bin
sNAME=PocketMine-MP
sDAEMON=/usr/local/PocketMine-MP/start.sh
sDAEMON_ARGS=""
sUSER=$sNAME # can't have - in usernmae
sCONFFILE=/usr/local/PocketMine-MP/server.properties
sPIDFILE=/var/run/$sNAME.pid
sCHDIR=/var/lib/$sNAME

# Load the VERBOSE setting and other rcS variables
. /lib/init/vars.sh
# Define LSB log_* functions.
# Depend on lsb-base (>= 3.0-6) to ensure that this file is present.
. /lib/lsb/init-functions

# Check if user exists
if ! id -u $sUSER > /dev/null 2>&1; then
    echo "The user does not exist; adapt and execute below commands to create it:"
    echo ""
    echo "root@sh1:~# adduser --home /usr/local/$sNAME/ --shell /bin/false --no-create-home --ingroup daemon --disabled-password --disabled-login $sUSER"
    echo "..."
    echo "root@sh1:~# chown $sNAME:daemon /usr/local/$sNAME/ -R"
    exit 1
fi

# Exit if the package is not installed
[ -x $sDAEMON ] || exit 0

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

# do_start()
# =========
# Function that starts the deamon/service
# Returns: 
#    0 if daemon has been started
#    1 if daemon was already running
#    2 if daemon could not be started
do_start() {
    if ! [ -f $sCONFFILE ]; then
        echo "$sNAME is not configured so not starting. Please create configuration file `$sCONFFILE`.">&2
        return 2
    fi

    # Directory in /var/run may disappear on reboot (e.g. when tmpfs used for /var/run).
    #mkdir -p $sRUNDIR
    #chown -R $sUSER: $sRUNDIR
    #chmod -R ug=rwX,o= $sRUNDIR

    # If using `start-stop-deamon` uncomment the below and comment `screen`
<<'COMMENT' # see https://unix.stackexchange.com/questions/37411/multiline-shell-script-comments-how-does-this-work
    start-stop-daemon --start --verbose --pidfile $sPIDFILE --exec $sDAEMON --name $sNAME --user $sUSER --test || return 1 # deamon is already running
    start-stop-daemon --start --verbose --pidfile $sPIDFILE --exec $sDAEMON --name $sNAME --user $sUSER --chdir $sCHDIR -- $sDAEMON_ARGS $sDAEMON_OPTS || return 2 # deamon could not be started
    return 0 # things worked
COMMENT

    # if using screen , #NOTE,2019-feb-26: using "" after user will error out as it treats it as a "command foo - bar" command
    sudo --user=$sUSER /usr/bin/screen -d -m -S $sNAME $sDAEMON $sDAEMON_ARGS

    sleep 5
    pgrep -f PocketMine-MP | tail -1 > $sPIDFILE 

}

# do_stop()
# =========
# Function that stops the daemon/service
# Reeturns:
#    0 if daemon has been stopped
#    1 if daemon was already stopped
#    2 if daemon could not be stopped
#    other if a failure occurred    
do_stop()
{
    # if using start-stop-deamon
#<<'COMMENT'
    #start-stop-daemon --stop --quiet --retry=TERM/30/KILL/5 --pidfile $sPIDFILE
    start-stop-daemon --stop --verbose --retry=TERM/30/KILL/5 --pidfile $sPIDFILE
    RETVAL="$?"
    [ "$RETVAL" = 2 ] && return 2
    # Wait for children to finish too if this is a daemon that forks
    # and if the daemon is only ever run from this initscript.
    # If the above conditions are not satisfied then add some other code
    # that waits for the process to drop all resources that could be
    # needed by services started subsequently.  A last resort is to
    # sleep for some time.
    #start-stop-daemon --stop --quiet --oknodo --retry=0/30/KILL/5 --exec $DAEMON
    #[ "$?" = 2 ] && return 2

    # Many daemons don\'t delete their pidfiles when they exit.
    rm -f $sPIDFILE
    sleep 5s
    return "$RETVAL"
#COMMENT

    # if using `screen` for interactive programs
}

# do_reload()
# ===========
# Function that sends a SIGHUP to the daemon/service
#
# If the daemon can reload its configuration without restarting (for example, when it is sent a SIGHUP), then implement that here.
do_reload() {

    # PocketMine does support SIGHUP https://github.com/PocketMine/PocketMine-MP/blob/master/src/pocketmine/Server.php#L2026
    #start-stop-daemon --stop --quiet --pidfile $sPIDFILE --name $sNAME --user $sUSER --signal HUP
    start-stop-daemon --stop --verbose --pidfile $sPIDFILE --name $sNAME --user $sUSER --signal HUP
}


case "$1" in
    start)
        [ "$VERBOSE" != no ] && log_daemon_msg "Starting \`$sDAEMON\` " "$sNAME"
        do_start
        case "$?" in
            0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
            2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
        esac
        ;;
    stop)
        [ "$VERBOSE" != no ] && log_daemon_msg "Stopping \`$sDAEMON\`" "$sNAME"
        do_stop
        case "$?" in
            0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
            2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
        esac
        ;;
    status)
        status_of_proc "$sDAEMON" "$sNAME" && exit 0 || exit $?
        ;;
    reload|force-reload)
        log_daemon_msg "Reloading \`$sDAEMON\`" "$sNAME"
        do_reload
        log_end_msg $?
        ;;
    restart)
        log_daemon_msg "Restarting \`$sDAEMON\`" "$sNAME"
        do_stop
        case "$?" in
            0|1)
                do_start
                case "$?" in
                    0) log_end_msg 0 ;;
                    1|*) log_end_msg 1 ;;
                esac
                ;;
            *) log_end_msg 1 ;;
        esac
        ;;
    *)
        echo "Usage: \`/etc/init.d/`basename "$0"` {start|stop|status|restart|force-reload}\`" >&2
        exit 3
        ;;
esac

exit 0

相关内容