메인 쉘이 종료된 후 >(프로세스)는 어떤 신호를 수신합니까?

메인 쉘이 종료된 후 >(프로세스)는 어떤 신호를 수신합니까?

Bash가 >(command) 구문(예: 프로세스 대체)을 사용하는 경우 솔루션을 제안할 수도 있지만 이는 Zshell 문제입니다. 이 매우 기본적인 코드는 모든 것을 설명합니다.

% fun() {
   setopt localtraps
   trap "echo waiting >> /tmp/out; sleep 2; echo bye >> /tmp/out; exit 1;" EXIT
   echo "My PID: $sysparams[pid]"  # needs zsh/system module
   repeat 1000; do
      read -r -t 1 line
   done
}

% exec {MYFD}> >(fun)
% exit

위의 작업은 fun()이 트랩을 수신하고 /tmp/out에 두 개의 메시지가 나타나고 "exit 1"이 프로세스를 종료한다는 것입니다.

내 질문: "EXIT"를 실제 신호로 대체할 수 있나요? PIPE, HUP, INT, TERM을 시도해 보았으나 아무 것도 작동하지 않았습니다.

답변1

귀하의 코드가 모든 것을 설명하지는 않습니다. 당신이 무엇을 하고 싶은지 모르겠어요. 그러나 제목의 질문에 답할 수 있습니다. >(…)메인 쉘이 종료되면 프로세스가 신호를 수신하지 않습니다. 내장 함수가 실행될 EXIT때까지 트랩을 실행하는 스크립트의 끝에 도달하기 때문에 종료됩니다 .exit

각 호출이 1초 정도 걸릴 것이라고 생각하기 때문에 스크립트가 일찍 종료될 것이라고 생각한다면 read -t 1, 그렇지 않습니다. 상위 프로세스가 종료되자마자 반환됩니다. 상위 프로세스가 종료되면 read하위 셸의 호출은 닫힌 파이프에서 데이터를 읽으려고 시도하며 기본 read시스템 호출은 사용 가능한 데이터 없이 즉시 반환됩니다.

답변2

bash및 매뉴얼에 따르면 zsh, 실제로 trap어떤 신호라도 보낼 수 있습니다.

bash:

   trap [-lp] [[arg] sigspec ...]
          The command arg is to be read and executed when the shell receives signal(s) sigspec.  If arg is absent (and there is a single sigspec) or  -,  each  specified  signal  is
          reset  to  its  original disposition (the value it had upon entrance to the shell).  If arg is the null string the signal specified by each sigspec is ignored by the shell
          and by the commands it invokes.  If arg is not present and -p has been supplied, then the trap commands associated with each sigspec are displayed.  If  no  arguments  are
          supplied  or  if  only -p is given, trap prints the list of commands associated with each signal.  The -l option causes the shell to print a list of signal names and their
          corresponding numbers.  Each sigspec is either a signal name defined in <signal.h>, or a signal number.  Signal names are case insensitive and the SIG prefix is  optional.

zsh:

   trap [ arg ] [ sig ... ]
          arg is a series of commands (usually quoted to protect it from immediate evaluation by the shell) to be read and executed when the shell receives any of the signals speci-
          fied by one or more sig args.  Each sig can be given as a number, or as the name of a signal either with or without the string SIG in front (e.g. 1, HUP,  and  SIGHUP  are
          all the same signal).

관련 정보