我在Postfix周围使用以下包装器在Docker容器(安装了bash和coreutils的Alpine 3.6)中运行它:
#!/usr/bin/env bash
#
# Starts Postfix and waits for its master process to exit.
set -eo pipefail
MASTER_PID=
main() {
# Start Postfix
postfix start
# Wait for Postfix to start
while [ ! -f /var/spool/postfix/pid/master.pid ]; do sleep 1; done
# Read the PID of Postfix's master process
MASTER_PID=$(cat /var/spool/postfix/pid/master.pid | tr -d '[:space:]')
readonly MASTER_PID
# Wait for Postfix to exit
tail -f /dev/null --pid "$MASTER_PID" &
wait "$!"
printf "Postfix Done\n"
}
shutdown() {
printf "Stopping Postfix\n"
# Stop Postfix
postfix stop
}
# Add signal handlers for stopping Postfix
trap shutdown SIGINT SIGTERM
main
我使用tail
等待Postfix的主进程退出,因为它不是执行脚本的shell的子进程。但是,tail
进程是并且我期望wait "$!"
等待尾部退出,这在Postfix的主进程退出时发生。这对于允许信号处理是必要的,它似乎有效:当发送SIGTERM
时,Postfix停止并且脚本终止。但是永远不会打印Postfix Done
。原因是什么?有趣的是,用
wait "$!"
时
TAIL_PID="$!"
while kill -0 "$TAIL_PID" 2>/dev/null; do sleep 1; done;
一切正常,Postfix Done
按预期打印。