将进程的生命周期与启动它的 shell 联系起来 [英] Tie the life of a process to the shell that started it

查看:20
本文介绍了将进程的生命周期与启动它的 shell 联系起来的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以 UNIX-y 方式,我尝试启动一个进程,将其置于后台,并将该进程的生命周期绑定到我的 shell.

In a UNIX-y way, I'm trying to start a process, background it, and tie the lifetime of that process to my shell.

我所说的不仅仅是后台处理进程,我希望向进程发送 SIGTERM,或者让它有一个关闭的打开文件描述符,或者 something 当shell 退出,因此 shell 的用户不必显式终止进程或收到您有正在运行的作业"警告.

What I'm talking about isn't simply backgrounding the process, I want the process to be sent SIGTERM, or for it to have an open file descriptor that is closed, or something when the shell exits, so that the user of the shell doesn't have to explicitly kill the process or get a "you have running jobs" warning.

最终我想要一个程序,它可以为每个 shell 唯一地运行,并与该 shell 一起携带状态,并在 shell 关闭时关闭.

Ultimately I want a program that can run, uniquely, for each shell and carry state along with that shell, and close when the shell closes.

IBM 的 DB2 控制台命令以这种方式工作.当您连接到数据库时,它会生成一个db2bp"进程,该进程携带数据库状态和连接并将其绑定到您的 shell.您可以在多个不同的终端或 ssh 连接中进行连接,每个都有自己的 db2bp 进程,当这些连接关闭时,相应的 db2bp 进程就会终止,并且该连接也将关闭.

IBM's DB2 console commands work this way. When you connect to the database, it spawns a "db2bp" process, that carries the database state and connection and ties it to your shell. You can connect in multiple different terminals or ssh connections, each with its own db2bp process, and when those are closed the appropriate db2bp process dies and that connection is closed.

然后使用 db2 命令启动 DB2 查询,该命令只是将其交给适当的 db2bp 进程.我不知道它是如何与正确 db2bp 进程通信的,但也许它使用连接到stdin 的tty 设备作为唯一键?我想我也需要弄清楚这一点.

DB2 queries are then started with the db2 command, which simply hands it off to the appropriate db2bp process. I don't know how it communicates with the correct db2bp process, but maybe it uses the tty device connected to stdin as a unique key? I guess I need to figure that out too.

我从来没有写过任何 tty 操作的东西,所以我什至不知道从哪里开始.如果我可以生成一个在 shell 退出时自动终止的进程,我想我可以解决剩下的问题.有谁知道 DB2 是怎么做到的?

I've never written anything that does tty manipulation, so I have no clue where to even start. I think I can figure the rest out if I can just spawn a process that is automatically killed on shell exit. Anyone know how DB2 does it?

推荐答案

如果你的shell不是子shell,你可以这样做;将以下内容放入名为ttywatch"的脚本中:

If your shell isn't a subshell, you can do the following; Put the following into a script called "ttywatch":

#!/usr/bin/perl
my $p=open(PI, "-|") || exec @ARGV; sleep 5 while(-t); kill 15,$p;

然后运行你的程序:

$ ttywatch commandline... & disown

disowning进程会防止shell报错有正在运行的进程,当终端关闭时,会导致SIGTERM(15)在5内下发到子进程(你的app)秒.

Disowning the process will prevent the shell from complaining that there are running processes, and when the terminal closes, it will cause SIGTERM (15) to be delivered to the subprocess (your app) within 5 seconds.

如果 shell 不是子 shell,您可以使用类似 ttywrap 至少给它自己的 tty,然后上面的技巧就会起作用.

If the shell isn't a subshell, you can use a program like ttywrap to at least give it its own tty, and then the above trick will work.

这篇关于将进程的生命周期与启动它的 shell 联系起来的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆