Programing

start-stop-daemon으로 시작한 프로세스의 stdout을 어떻게 기록 할 수 있습니까?

lottogame 2020. 7. 21. 21:30
반응형

start-stop-daemon으로 시작한 프로세스의 stdout을 어떻게 기록 할 수 있습니까?


간단한 프로세스를 실행하기 위해 init 스크립트를 사용하고 있습니다.

start-stop-daemon --start --quiet --chuid $DAEMONUSER    \
    --make-pidfile --pidfile $PIDFILE --background       \
    --exec $DAEMON $DAEMON_ARGS

$ DAEMON이라는 프로세스는 일반적으로 표준 출력에 로그 정보를 인쇄합니다. 내가 알 수있는 한이 데이터는 어디에도 저장되지 않습니다.

$ DAEMON의 stdout을 파일 어딘가에 쓰거나 추가하고 싶습니다.

내가 아는 유일한 해결책은 start-stop-daemon에게 $ DAEMON 대신 직접 쉘 스크립트를 호출하도록 지시하는 것입니다. 그런 다음 스크립트는 $ DAEMON을 호출하고 로그 파일에 씁니다. 그러나 데몬 자체를 수정하는 것과 같이 일반적인 작업을 해결하는 잘못된 방법 인 것처럼 보이는 추가 스크립트가 필요합니다.


ypocat의 답변을 확장하려면 다음과 같이 설명하지 않습니다.

start-stop-daemon --start --quiet --chuid $DAEMONUSER    \
 --make-pidfile --pidfile $PIDFILE --background       \
 --startas /bin/bash -- -c "exec $DAEMON $DAEMON_ARGS > /var/log/some.log 2>&1"

exec데몬을 실행하는 데 사용 하면 bash 부모 대신 자식 프로세스를 올바르게 중지 할 수 있습니다.

사용 --startas대신 --exec프로세스가 올바르게 pid로 감지하고 시작을 여러 번 호출하면 잘못 데몬의 여러 인스턴스를 시작하지 않을 것을 보장한다. 그렇지 않으면 start-stop-daemon은 / bin / bash 프로세스를 찾고 데몬을 실행하는 실제 자식 프로세스를 무시합니다.


해야 할 일 :

start-stop-daemon --start --quiet --chuid $DAEMONUSER    \
    --make-pidfile --pidfile $PIDFILE --background       \
    --exec /bin/bash -- -c "$DAEMON $DAEMON_ARGS > /var/log/some.log 2>&1"

당신이 사용하는 경우 또한 --chuid또는 --user, 반드시 사용자가 쓸 수 있도록 /var/log하거나 기존 /var/log/some.log. 가장 좋은 방법은 해당 사용자가 소유하는 /var/log/subdir/것입니다.


데몬 출력 캡처를 --no-close시작할 때 매개 변수 를 사용할 수 있어야합니다 start-stop-daemon. 새로운 기능dpkg데비안 버전 1.16.5 이후 패키지 에서 사용 가능합니다 :

--background에서 fds 닫기를 비활성화하려면 새로운 --no-close 옵션을 추가하십시오.

이를 통해 호출자는 디버깅 목적으로 프로세스 메시지를 보거나 파일 디스크립터를 로그 파일, syslog 또는 이와 유사한 것으로 경로 재 지정할 수있었습니다.


openrc (예를 들어 gentoo 또는 alpine linux의 기본값) start-stop-daemon에는 -1-2옵션이 있습니다.

-1, --stdout stdout을 파일로 리디렉션

-2, --stderr stderr를 파일로 리디렉션

그래서 당신은 쓸 수 있습니다 :

start-stop-daemon --start --quiet --chuid $DAEMONUSER    \
    --make-pidfile --pidfile $PIDFILE --background       \
    --exec $DAEMON $DAEMON_ARGS -1 $LOGFILE -2 $LOGFILE

데몬의 출력을 캡처하여 파일로 저장하는 것은 그리 어렵지 않습니다.

start-stop-daemon --start --background \
  --pidfile $PIDFILE --make-pidfile \
  --chuid $DAEMON_USER \
  --startas $DAEMON --no-close \
  -- $DAEMON_ARGS >> $LOGFILE 2>&1

그러나이 솔루션은 적합하지 않을 수 있습니다 logrotate.

출력을 syslog로 캡처하는 것이 좋습니다. 데비안 이 systemd 서비스의 동작을 일치합니다. 위의 예제를 다시 작성하려는 다음과 같은 간단한 시도는 데몬을 중지 한 후 자식 만 종료하고 모든 자손을 종료하지 않기 때문에 두 개의 부모없는 프로세스 (로거 및 데몬)를 남기므로 잘못된 것입니다start-stop-daemon .

## Do not use this!
start-stop-daemon --start --background \
  --pidfile $PIDFILE --make-pidfile \
  --chuid $DAEMON_USER \
  --startas /bin/sh \
  -- -c """exec $DAEMON $DAEMON_ARGS | /usr/bin/logger --tag $NAME"""

그것을 작동시키기 위해서는 우리 SIGTERM로부터 받는 자식을 종료하는 래퍼가 필요합니다 start-stop-daemon. 몇 가지가 있습니다 :

duende :
start-stop-daemon --start --background \
  --pidfile $PIDFILE \
  --startas /usr/sbin/duende \
  -- --pid $PIDFILE --chroot=/ --uid 65534 --ident $NAME \
  /bin/su --login $DAEMON_USER --shell /bin/sh --command """exec ${DAEMON} $DAEMON_ARGS"""

참고 : uid=65534사용자 nobody입니다.

장점 : 작동하며 비교적 쉽습니다.
단점 : 4 프로세스 (감독자 duende, 권한이 떨어지는 포크 (로거) su및 데몬 자체); 필수 --chroot; 데몬이 즉시 종료되면 (예 : 잘못된 명령) status_of_proc -p $PIDFILE "$DAEMON" "$NAME"성공적으로 시작된 것으로보고하십시오.

데몬 :
start-stop-daemon --start --pidfile $PIDFILE \
  --startas /usr/bin/daemon \
  -- --noconfig --name $NAME --stderr=syslog.info --stdout=syslog.info \
  -- /bin/su --login $DAEMON_USER --shell /bin/sh --command """exec $DAEMON $DAEMON_ARGS"""

장점 : 3 개 과정 (관리자 daemon, su자체 데몬).
단점 : $PIDFILE혼란스러운 데몬 의 명령 줄 옵션 으로 인해 관리하기가 어렵습니다 . 데몬이 즉시 종료되면 (예 : 잘못된 명령) status_of_proc -p $PIDFILE "$DAEMON" "$NAME"성공적으로 시작된 것으로보고하십시오.

pipexec ( 우승자 ) :

start-stop-daemon --start --background \
  --pidfile $PIDFILE --make-pidfile \
  --chuid $DAEMON_USER \
  --startas /usr/bin/pipexec -- -k \
   -- [ D $DAEMON $DAEMON_ARGS ] [ L /usr/bin/logger --tag $NAME ] '{D:2>D:1}' '{D:1>L:0}'

Pros: 3 processes (supervisor pipexec, logger and daemon itself); If daemon terminates right away (e.g. invalid command) status_of_proc -p $PIDFILE "$DAEMON" "$NAME" correctly report failure.
Cons: none.

This is the winner -- the easiest, neat solution that seems to be working well.


Usually start-stop-daemon closes the standard file descriptors when running in the background. From the man page of start-stop-daemon:

-C, --no-close
Do not close any file descriptor when forcing the daemon into the background. Used for debugging purposes to see the process output, or to redirect file descriptors to log the process output. Only relevant when using --background.

This one worked for me:

    start-stop-daemon -b -C -o -c \ 
         $DAEMON_USER -S -x $DAEMON > $DAEMON_LOG 2>&1

Quoting an old mailing list:

https://lists.ubuntu.com/archives/ubuntu-uk/2005-June/000037.html

An easy -- and if you want to use start-stop-daemon perhaps the only -- way around it is to create a small script containing:

#!/bin/sh
exec /home/boinc/boinc/boinc > /home/boinc/log/boinc.log

and then use that script as the argument to start-stop-daemon.

Perhaps the real question however is whether it is really necessary to use start-stop-daemon in the first place?


I'm not sure if "$DAEMON $DAEMON_ARGS > /var/log/some.log 2>&1" will ever close the file descriptor for the log file... which means if your daemon runs forever, I'm not sure that logrotate or other mechanisms for cleaning up disk space would work. Since it's > instead of >>, the suggested command would also truncate existing logs on restart. If you want to see why the daemon crashed, and it restarts automatically, that might not be very helpful.

Another option might be "$DAEMON | logger". logger is a command that will log to syslog (/var/log/messages). If you need stderr too, I think you could use "$DAEMON 1>&2 | logger"


Assuming it's bash (although some other shells may allow this as well), the line:

exec >>/tmp/myDaemon.log

will send all future standard output to that file. That's because exec without a program name just does some redirection magic. From the bash man page:

If command is not specified, any redirections take effect in the current shell.

Management of said file is another issue of course.


How about:

sudo -u myuser -i start-stop-daemon ...

참고URL : https://stackoverflow.com/questions/8251933/how-can-i-log-the-stdout-of-a-process-started-by-start-stop-daemon

반응형