我正在从 bash shell 中执行一个进程(流式服务器),该进程又启动一个子(媒体播放器)进程。问题是当我关闭孩子时,父母会继续运行。
我无法访问他们的资源,但我想知道是否有办法将它们包装在祖父母脚本/进程中,所以当我关闭孩子时,父母也会关闭?
任何指导将不胜感激,在此先感谢。
The parent probably launches the child by executing a file. Say that file is /blah/blah/child
. What you can do is rename /blah/blah/child
and instead have a carefully-crafted shell-script with that name. The shell script can call the renamed child passing through all arguments. When the child exits, the shell-script can kill the parent-process ($PPID).
Something like this:
$ mv -i /blah/blah/child /blah/blah/child-renamed
$ cat > /blah/blah/child
#!/bin/sh
/blah/blah/child-renamed "$@"
kill -TERM $PPID
^D
chmod +x /blah/blah/child # Actually, you should match the permissions from /blah/blah/child-renamed
This may not actually work if the parent has done something odd but you might be lucky.
Another complication I've just thought of: If the child process changes its behaviour depending on the name of the file it is executed from, then you are going to have to work a bit harder to make this work properly.
通常,您可以使用命令ps -ef
来检查您要杀死的进程ID及其父ID,然后您可以使用命令“kill -pid”来杀死您要杀死的进程。