服务不起作用

服务不起作用

背景

  • 我在用这个脚本为了使我的脚本作为服务运行。
  • 我正在使用 Linux Mint 17.3 Rosa。

问题

有时启动/停止有效,有时无效。有时,我必须手动终止该进程,因为服务停止命令不会执行此操作。

请注意,该脚本使用该daemon工具。

#!/bin/sh

name="TEST"  
# The path of the client executable
command="${HOME}/myscript.sh"   
# Any command line arguments for the client executable
command_args=""  
# The path of the daemon executable
daemon="/usr/bin/daemon"

[ -x "$daemon" ] || exit 0
[ -x "$command" ] || exit 0

# Note: The following daemon option arguments could be in /etc/daemon.conf


# Any command line arguments for the daemon executable (when starting)
daemon_start_args="" # e.g. --inherit --env="ENV=VAR" --unsafe  

# The pidfile directory (need to force this so status works for normal users)
pidfiles="/var/run"  
# The user[:group] to run as (if not to be run as root)
user=""  
# The path to chroot to (otherwise /)
chroot=""  
# The path to chdir to (otherwise /)
chdir=""  
# The umask to adopt, if any
umask=""  
# The syslog facility or filename for the client's stdout (otherwise discarded)
stdout="daemon.info"  
# The syslog facility or filename for the client's stderr (otherwise discarded)
stderr="daemon.err"

case "$1" in  
    start)
        # This if statement isn't strictly necessary but it's user friendly
        if "$daemon" --running --name "$name" --pidfiles "$pidfiles"
        then
            echo "$name is already running."
        else
            echo -n "Starting $name..."
            "$daemon" --respawn $daemon_start_args \
                --name "$name" --pidfiles "$pidfiles" \
                ${user:+--user $user} ${chroot:+--chroot $chroot} \
                ${chdir:+--chdir $chdir} ${umask:+--umask $umask} \
                ${stdout:+--stdout $stdout} ${stderr:+--stderr $stderr} \
                -- \
                "$command" $command_args
            echo done.
        fi
        ;;

    stop)
        # This if statement isn't strictly necessary but it's user friendly
        if "$daemon" --running --name "$name" --pidfiles "$pidfiles"
        then
            echo -n "Stopping $name..."
            "$daemon" --stop --name "$name" --pidfiles "$pidfiles"
            echo done.
        else
            echo "$name is not running."
        fi
        ;;

    restart|reload)
        if "$daemon" --running --name "$name" --pidfiles "$pidfiles"
        then
            echo -n "Restarting $name..."
            "$daemon" --restart --name "$name" --pidfiles "$pidfiles"
            echo done.
        else
            echo "$name is not running."
            exit 1
        fi
        ;;

    status)
        "$daemon" --running --name "$name" --pidfiles "$pidfiles" --verbose
        ;;

    *)
        echo "usage: $0 <start|stop|restart|reload|status>" >&2
        exit 1
esac

exit 0  
myscript.sh:
#!/bin/bash

cd $HOME/Stuff/
$(which node) app.js 

尝试解决这个问题

1.使用启动-停止-守护进程

作为替代方案,我尝试使用它使用start-stop-daemon但它根本不运行。

2.调试daemon脚本

为了尝试调试这个,我在daemon命令中启用了调试。然后我尝试启动和停止我的服务。

当我启动服务时:

Starting TEST...daemon: debug: handle_stdout_option(spec = daemon.info)
daemon: debug: store_syslog(spec = daemon.info)
daemon: debug: handle_stderr_option(spec = daemon.err)
daemon: debug: store_syslog(spec = daemon.err)
daemon: debug: config()
daemon: debug: config_load(configfile = /etc/daemon.conf)
daemon: debug: config_load(configfile = /root/.daemonrc)
daemon: debug: config_process(target = *)
daemon: debug: config_process(target = TEST)
daemon: debug: handle_name_option(spec = TEST)
daemon: debug: handle_pidfiles_option(spec = /var/run)
daemon: debug: handle_stdout_option(spec = daemon.info)
daemon: debug: store_syslog(spec = daemon.info)
daemon: debug: handle_stderr_option(spec = daemon.err)
daemon: debug: store_syslog(spec = daemon.err)
daemon: debug: sanity_check()
daemon: debug:  preventing core files
daemon: debug:  constructing pidfile path
daemon: debug:  constructing command line arguments for the client
daemon: debug:  checking the client command
daemon: debug: safety_check("/home/myPC/myscript.sh")
daemon: debug:  checking "/home/myPC/myscript.sh"
daemon: debug:  checking #! interpreter: /bin/bash
TEST: debug:  becoming a daemon and locking pidfile
done.

并且服务已经启动。

当我停止服务时:

Stopping TEST...daemon: debug: config()
daemon: debug: config_load(configfile = /etc/daemon.conf)
daemon: debug: config_load(configfile = /root/.daemonrc)
daemon: debug: config_process(target = *)
daemon: debug: config_process(target = TEST)
daemon: debug: handle_name_option(spec = TEST)
daemon: debug: handle_pidfiles_option(spec = /var/run)
daemon: debug: sanity_check()
daemon: debug:  preventing core files
daemon: debug:  constructing pidfile path
daemon: debug: show()
daemon: debug:  options:
daemon: debug:   config <none>, noconfig 0, name TEST, command "<none>", uid 0, gid 0, init_groups 0, chroot <none>, chdir <none>, umask 22, inherit no, respawn no, acceptable 300, attempts 5, delay 300, limit 0, idiot 0, foreground no, pty no, noecho no, stdout <none>, stderr <none>, errlog daemon.err, dbglog daemon.debug, core no, unsafe no, safe no, stop yes, running no, verbose 0, debug 3
daemon: debug:  command line:
daemon: debug:   environment:
daemon: debug:    SUDO_GID=1000
daemon: debug:    MAIL=/var/mail/root
daemon: debug:    USER=root
daemon: debug:    HOME=/home/myPC
daemon: debug:    LC_MONETARY=en_US.UTF-8
daemon: debug:    COLORTERM=mate-terminal
daemon: debug:    SUDO_UID=1000
daemon: debug:    LOGNAME=root
daemon: debug:    TERM=xterm
daemon: debug:    USERNAME=root
daemon: debug:    PATH=.../home/myPC/bin:/usr/bin:/usr/local/bin:/usr/local/sbin:/usr/sbin:/sbin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
daemon: debug:    LC_ADDRESS=en_US.UTF-8
daemon: debug:    DISPLAY=:0.0
daemon: debug:    LC_TELEPHONE=en_US.UTF-8
daemon: debug:    LANG=en_US.UTF-8
daemon: debug:    LS_COLORS=rs=0:di=01;34:ln=01;36:mh=00:pi=40;33:so=01;35:do=01;35:bd=40;33;01:cd=40;33;01:or=40;31;01:su=37;41:sg=30;43:ca=30;41:tw=30;42:ow=34;42:st=37;44:ex=01;32:*.tar=01;31:*.tgz=01;31:*.arj=01;31:*.taz=01;31:*.lzh=01;31:*.lzma=01;31:*.tlz=01;31:*.txz=01;31:*.zip=01;31:*.z=01;31:*.Z=01;31:*.dz=01;31:*.gz=01;31:*.lz=01;31:*.xz=01;31:*.bz2=01;31:*.bz=01;31:*.tbz=01;31:*.tbz2=01;31:*.tz=01;31:*.deb=01;31:*.rpm=01;31:*.jar=01;31:*.war=01;31:*.ear=01;31:*.sar=01;31:*.rar=01;31:*.ace=01;31:*.zoo=01;31:*.cpio=01;31:*.7z=01;31:*.rz=01;31:*.jpg=01;35:*.jpeg=01;35:*.gif=01;35:*.bmp=01;35:*.pbm=01;35:*.pgm=01;35:*.ppm=01;35:*.tga=01;35:*.xbm=01;35:*.xpm=01;35:*.tif=01;35:*.tiff=01;35:*.png=01;35:*.svg=01;35:*.svgz=01;35:*.mng=01;35:*.pcx=01;35:*.mov=01;35:*.mpg=01;35:*.mpeg=01;35:*.m2v=01;35:*.mkv=01;35:*.webm=01;35:*.ogm=01;35:*.mp4=01;35:*.m4v=01;35:*.mp4v=01;35:*.vob=01;35:*.qt=01;35:*.nuv=01;35:*.wmv=01;35:*.asf=01;35:*.rm=01;35:*.rmvb=01;35:*.flc=01;35:*.avi=01;35:*.fli=01;35:*.flv=01;35:*.gl=01;35:*.dl=01;35:*.xcf=01;35:*.xwd=01;35:*.yuv=01;35:*.cgm=01;35:*.emf=01;35:*.axv=01;35:*.anx=01;35:*.ogv=01;35:*.ogx=01;35:*.aac=00;36:*.au=00;36:*.flac=00;36:*.mid=00;36:*.midi=00;36:*.mka=00;36:*.mp3=00;36:*.mpc=00;36:*.ogg=00;36:*.ra=00;36:*.wav=00;36:*.axa=00;36:*.oga=00;36:*.spx=00;36:*.xspf=00;36:
daemon: debug:    XAUTHORITY=/home/myPC/.Xauthority
daemon: debug:    SUDO_COMMAND=/usr/bin/env PATH=.../home/myPC/bin:/usr/bin:/usr/local/bin:/usr/local/sbin:/usr/sbin:/sbin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin /etc/init.d/moda stop
daemon: debug:    LC_NAME=en_US.UTF-8
daemon: debug:    SHELL=/bin/bash
daemon: debug:    SUDO_USER=myPC
daemon: debug:    LC_MEASUREMENT=en_US.UTF-8
daemon: debug:    LC_IDENTIFICATION=en_US.UTF-8
daemon: debug:    PWD=/home/myPC
daemon: debug:    LC_NUMERIC=en_US.UTF-8
daemon: debug:    LC_PAPER=en_US.UTF-8
daemon: debug:  stopping daemon /var/run/TEST.pid
done.

但服务永远不会停止。

即使我尝试sudo kill -9 PID它也不会总是停止(有时它会停止使用它,但并非始终如一)。

我的调试过程

我正在运行该服务,例如:

 sudo /etc/init.d/test start

ps然后我使用和来观察它lsof

附注
$ ps aux |grep TEST
root      4393  0.0  0.1  20036  1348 ?        S    13:41   0:00 /usr/bin/daemon --respawn --name TEST --pidfiles /var/run --debug=3 --stdout daemon.info --stderr daemon.err -- /home/myPC/myscript.sh
root      4394  0.0  0.0      0     0 ?        Z    13:41   0:00 [myscript.sh] <defunct>
拉索夫
$ sudo lsof -p 4393
lsof: WARNING: can't stat() fuse.gvfsd-fuse file system /run/user/1000/gvfs
      Output information may be incomplete.
COMMAND  PID USER   FD   TYPE             DEVICE SIZE/OFF   NODE NAME
daemon  4393 root  cwd    DIR                8,2     4096      2 /
daemon  4393 root  rtd    DIR                8,2     4096      2 /
daemon  4393 root  txt    REG                8,2   190280 923071 /usr/bin/daemon
daemon  4393 root  mem    REG                8,2    43616 667389 /lib/x86_64-linux-gnu/libnss_files-2.19.so
daemon  4393 root  mem    REG                8,2    47760 667228 /lib/x86_64-linux-gnu/libnss_nis-2.19.so
daemon  4393 root  mem    REG                8,2    97296 667121 /lib/x86_64-linux-gnu/libnsl-2.19.so
daemon  4393 root  mem    REG                8,2    39824 667115 /lib/x86_64-linux-gnu/libnss_compat-2.19.so
daemon  4393 root  mem    REG                8,2  2924352 927252 /usr/lib/locale/locale-archive
daemon  4393 root  mem    REG                8,2  1840928 667355 /lib/x86_64-linux-gnu/libc-2.19.so
daemon  4393 root  mem    REG                8,2    10680 667382 /lib/x86_64-linux-gnu/libutil-2.19.so
daemon  4393 root  mem    REG                8,2   141574 667155 /lib/x86_64-linux-gnu/libpthread-2.19.so
daemon  4393 root  mem    REG                8,2   149120 667163 /lib/x86_64-linux-gnu/ld-2.19.so
daemon  4393 root    0u   CHR                1,3      0t0   1029 /dev/null
daemon  4393 root    1u   CHR                1,3      0t0   1029 /dev/null
daemon  4393 root    2u   CHR                1,3      0t0   1029 /dev/null
daemon  4393 root    3uW  REG               0,17        5  22031 /run/TEST.pid
daemon  4393 root    4u  unix               0xf..     0t0  22032 socket
daemon  4393 root    7r  FIFO                0,9      0t0  22034 pipe
daemon  4393 root    9r  FIFO                0,9      0t0  22035 pipe

问题

我注意到的一件事是,在上面的信息中,我可以看到,/run/TEST.pid但这也显示在这里:

/var/run/TEST.pid

守护程序脚本仅使用该/var/run目录。我不确定这是否应该发生?

相关内容