使用有啟動停止守護程序的螢幕,sysv

使用有啟動停止守護程序的螢幕,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名稱或參數中包含 as 的任何其他進程。

我認為我的解決方案是讓我的ps函數僅返回$NAME由 . 創建的進程名稱的 pid $USER。由於該守護程序將在專用使用者 ID 下運行。

我不確定如何獲得這個輸出。ps -C $NAME -u $USER o pid=為我提供了每場比賽的列表,但我想要兩場比賽都有一個列表。如果我決定該用戶稍後可以處理其他進程。

另外,重載怎麼辦? start-stop-daemon --stop --signal 1 --quiet --pidfile $PIDFILE --name $NAME會重新載入screen進程,會重新載入守護進程嗎?有沒有更好的辦法?非常感謝任何幫助。

答案1

我需要同樣的 Minecraft 基岩伺服器作為守護進程,但我的鴨鴨豆腐未能找到解決方案...最終,儘管我組裝了以下初始化腳本。

需要注意的重點是start-stop-deamon不是用來啟動screen會話的,我們運行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

相關內容