大网数据广告活动
服务器运维问答

Parent bash script not receiving 'trap' despite process still running

· 52CE 我爱测

What I'm actually trying to achieve: I'm trying to get a custom daemon working on a system that uses SysVinit. I have the bootstrapper /etc/init.d/xyz script already, which calls my daemon, but it doesn't automatically place it i…

问题原文

原帖链接 · Lawrence Wagerfield · CC BY-SA 3.0

What I'm actually trying to achieve:

I'm trying to get a custom daemon working on a system that uses SysVinit. I have the bootstrapper /etc/init.d/xyz script already, which calls my daemon, but it doesn't automatically place it in the background. This is similar to how services like nginx behave: the binary backgrounds itself - i.e. it's not the responsibility of the /etc/init.d/nginx script to daemonise the process, so if you ran /opt/nginx/sbin/nginx directly you would also experience daemonised/background execution.

The Problem

My problem is that using my current method, the daemon doesn't terminate with the parent process (which is what gets terminated when you call service xyz stop).

I'm using a parent launcher.sh script that runs a daemon.sh & script. However, when I kill launcher.sh the daemon.sh continues to run, despite my best efforts with trap (it simply never gets called):

-> launcher.sh

#!/bin/bash

function shutdown {
    # Get our process group id
    PGID=$(ps -o pgid= $$ | grep -o [0-9]*)

    echo THIS NEVER GETS CALLED!

    # Kill process group in a new process group
    setsid kill -- -$$
    exit 0
}

trap "shutdown" SIGTERM

# Run daemon in background

./daemon.sh &

-> daemon.sh

#!/bin/bash

while true
do
    sleep 1
done

To run & kill:

./launcher.sh

<get PID for launcher>

kill -TERM 123 # PID of launcher.sh... which _is_ still running and has its own PID.

Result: daemon.sh still running and the shutdown function never gets called - I've confirmed this before by placing an echo here in the function body.

Any ideas?

EDIT: The launcher.sh script is being run using daemon launcher.sh, where daemon is a function provided by Amazon Linux's init.d/functions file (see here: http://gist.github.com/ljwagerfield/ab4aed16878dd9a8241b14bc1501392‌​f).

已采纳解答

原帖链接 · J Earls · CC BY-SA 3.0

The trap command only works as long as the script is running.

The way this is normally done is that, when the daemon is forked off, it writes its PID into a file. The init script then either uses that file to determine what process to kill, or calls your launcher script to kill the process.

For the first instance:

launcher.sh:

/path/to/daemon.sh &
echo "$!" > /var/run/xyz.pid

A simple and somewhat naieve version of /etc/init.d/xyz:

# ... pull in functions or sysconfig files ...
start() {
    # ... do whatever is needed to set things up to start ...
    /path/to/launcher.sh
}
stop() {
    # ... do whatever is needed to set things up to stop ...
    kill `cat /var/run/xyz.pid`
}
# ... other functions ...

A non-naive startup script will depend on which version of linux you're running; I would suggest looking at other examples in /etc/init.d to see how they do this.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2016-09-15)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。