Detaching Bash And Going Home

Detaching a bash process

Have you ever started a process not realizing how long it was going to take and needed to leave? There are several ways to accomplish this. Two of the best choices are to use screen or tmux in the first place. They are both terminal multiplexers that allow you to run multiple virtual terminals at the same time. The most relevant feature to this post is that you can disconnect and reconnect terminal sessions. I already use xmonad as a window manager and run multiple "windows" with vim so the overhead of one more multiple "windows" thing doesn't really appeal to me. That said, they both do quite a number of things that are extremely useful. If you use them, you will have better ways of disconnecting a process than the advice offered here.

How to disconnect a bash process and log out without killing it

Start up a long running process. In this case, I've started a rake task that sleeps for a while. Hit ctrl+z to suspend it. Put it in the background with bg, and check that it's running with jobs.

1 abc1231@devserver (master %) $ rake longrun
2 ^Z
3 [1]+  Stopped                 rake longrun
4 abc1231@devserver (master %) $ bg %1
5 [1]+ rake longrun &
6 abc1231@devserver (master %) $ jobs
7 [1]+  Running                 rake longrun &

Find our process. Note that it's on pts/4. It's pid is 21646.

 1 abc1231@devserver (master %) $ ps -ef --forest
 2 ...
 3 root      1192     1  0 Jul19 ?        00:00:00 /usr/sbin/sshd -D
 4 root     30955  1192  0 Jul25 ?        00:00:00  \_ sshd: abc1231 [priv]
 5 abc1231  31030 30955  0 Jul25 ?        00:00:05  |   \_ sshd: abc1231@pts/3 
 6 abc1231  31031 31030  0 Jul25 pts/3    00:00:00  |       \_ -bash
 7 abc1231  20602 31031  0 15:18 pts/3    00:00:23  |           \_ vi _posts/
 8 root     31478  1192  0 Jul25 ?        00:00:00  \_ sshd: abc1231 [priv]
 9 abc1231  31554 31478  0 Jul25 ?        00:00:01  |   \_ sshd: abc1231@pts/4 
10 abc1231  31555 31554  0 Jul25 pts/4    00:00:00  |       \_ -bash
11 abc1231  21646 31555  0 16:30 pts/4    00:00:00  |           \_ ruby /home/abc1231/.rvm/gems/ruby-1.9.2-p136/bin/rake longrun
12 abc1231  21792 31555  0 16:33 pts/4    00:00:00  |           \_ ps -ef --forest
13 ...

We're on pts/4.

1 elon@devserver (master %) $ who
2 elon  pts/0        2012-07-28 15:38 (192.168.56.1)
3 elon  pts/3        2012-07-16 19:25 (192.168.56.1)
4 elon  pts/4        2012-07-16 19:48 (192.168.56.1)

List the jobs again. Disown the long running job. Verify that it is no longer listed as a job. Exit.

1 abc1231@devserver (master %) $ jobs
2 [1]+  Running                 rake longrun &
3 abc1231@devserver (master %) $ disown %1
4 abc1231@devserver (master %) $ jobs
5 abc1231@devserver (master %) $ exit

Log back in. Recheck the running processes. Note that the process is still going, but it is no longer associated with our pts.

 1 abc1231@devserver (master %) $ ps -ef --forest
 2 ...
 3 root      1192     1  0 Jul19 ?        00:00:00 /usr/sbin/sshd -D
 4 root     30955  1192  0 Jul25 ?        00:00:00  \_ sshd: abc1231 [priv]
 5 abc1231  31030 30955  0 Jul25 ?        00:00:05  |   \_ sshd: abc1231@pts/3 
 6 abc1231  31031 31030  0 Jul25 pts/3    00:00:00  |       \_ -bash
 7 abc1231  20602 31031  0 15:18 pts/3    00:00:24  |           \_ vi _posts/
 8 root     31478  1192  0 Jul25 ?        00:00:00  \_ sshd: abc1231 [priv]
 9 abc1231  31554 31478  0 Jul25 ?        00:00:01  |   \_ sshd: abc1231@pts/4 
10 abc1231  31555 31554  0 Jul25 pts/4    00:00:00  |       \_ -bash
11 abc1231  21874 31555  0 16:34 pts/4    00:00:00  |           \_ ps -ef --forest
12 ...
13 abc1231  21646     1  0 16:30 ?        00:00:01 ruby /home/abc1231/.rvm/gems/ruby-1.9.2-p136/bin/rake longrun
14 ...

Most of this exercise has been for illustration. It can be simplified to this:

  • run the process
  • suspend with ctrl+z
  • bg %jobnumber (i.e. %1)
  • disown %jobnumber
  • optionally exit
comments powered by Disqus