Tie the life of a process to the shell that started it

好久不见. 提交于 2019-12-04 04:44:44

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.

Okay, I think I figured it out. I was making it too complicated :)

I think all db2 is daemon-izing db2bp, then db2bp is calling waitpid on the parent PID (the shell's PID) and exiting after waitpid returns.

The communication between the db2 command and db2bp seems to be done via fifo with a filename based on the parent shell PID.

Waaaay simpler than I was thinking :)

For anyone who is curious, this whole endeavor was to be able to tie a python or groovy interactive session to a shell, so I could test code while easily jumping in and out of a session that would retain database connections and temporary classes / variables.

Thank you all for your help!

Your shell should be sending a SIGHUP signal to any running child processes when it shuts down. Have you tried adding a SIGHUP handler to your application to shut it down cleanly when the shell exits?

dmckee

Is it possible that your real problem here is the shell and not your process. My understanding agrees with Jim Lewis' that when the shell dies its children should get SIGHUP. But what you're complaining about is the shell (or perhaps the terminal) trying to prevent you from accidentally killing a running shell with active children.

Consider reading the manual for the shell or the terminal to see if this behavior is configurable.

From the bash manual on my MacBook:

The shell exits by default upon receipt of a SIGHUP. Before exiting, an interactive shell resends the SIGHUP to all jobs, running or stopped. Stopped jobs are sent SIGCONT to ensure that they receive the SIGHUP. To prevent the shell from sending the signal to a particular job, it should be removed from the jobs table with the disown builtin (see SHELL BUILTIN COMMANDS below) or marked to not receive SIGHUP using disown -h.

If the huponexit shell option has been set with shopt, bash sends a SIGHUP to all jobs when an interactive login shell exits.

which might point you in the right direction.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!