将一个进程的生命绑定到启动它的shell中 [英] Tie the life of a process to the shell that started it

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

问题描述



在UNIX-y中,我正在尝试启动一个进程,将其背景化,并将该进程的生命周期与我的shell绑定。我说的不仅仅是这个过程的背景,我希望这个进程被发送给SIGTERM,或者当它退出时,它有一个打开的文件描述符被关闭,或者 shell的用户不必明确地删除该进程,或者获得一个你有运行的作业警告。



最终我想要一个可以独立运行的程序,对于每个shell,并与该shell一起携带状态,并在shell关闭时关闭。



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



DB2然后使用db2命令启动查询,该命令可以将其交给适当的db2bp进程。我不知道如何与正确的 db2bp进程进行通信,但也可能使用连接到stdin的tty设备作为唯一的密钥?我想我也需要弄清楚。



我从来没有写过任何有tty操作的东西,所以我没有线索甚至开始。我想我可以计算出来,如果我能产生一个在shell退出时被自动杀死的进程。任何人都知道DB2如何做?

解决方案

如果你的shell不是子shell,你可以执行以下操作:将以下内容放入一个名为ttywatch的脚本中:

 #!/ usr / bin / perl 
我的$ p = open(PI, - |)|| exec @ARGV;睡觉5((t))杀15,$ p;

然后运行您的程序:

  $ ttywatch commandline ...&拒绝

取消该进程将阻止shell抱怨正在运行进程,当终端关闭,这将导致SIGTERM( 15 )在5秒内传送到子进程(您的应用程序)。



如果shell不是子shell,则可以使用 ttywrap 等程序至少给它自己的tty,然后上面的技巧将会奏效。


In a UNIX-y way, I'm trying to start a process, background it, and tie the lifetime of that process to my 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.

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'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 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.

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?

解决方案

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;

Then run your program as:

$ ttywatch commandline... & disown

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.

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天全站免登陆