title: Tie the life of a process to the shell that started it
body: <p>In a UNIX-y way, I'm trying to start a process, background it, and tie the lifetime of that process to my shell.</p>
  
  <p>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 <em>something</em> 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.</p>
  
  <p>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.</p>
  
  <p>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.</p>
  
  <p>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 <em>correct</em> 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.</p>
  
  <p>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?</p>