배경
- 나는 사용하고있다이 스크립트내 스크립트를 서비스로 실행하려면
- 저는 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
전혀 실행되지 않습니다.
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
그런 다음 and 를 사용하여 관찰합니다 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
디렉터리만 사용합니다. 이런 일이 일어나야 할지 잘 모르겠습니다.