Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detach a process from terminal in unix?

Tags:

unix

When I start a process in background in a terminal and some how if terminal gets closed then we can not interact that process any more. I am not sure but I think process also get killed. Can any one please tell me how can I detach that process from my terminal. So even if I close terminal then I can interact with same process in new terminal ?

I am new to unix so your extra information will help me.

like image 670
Abhishek Gupta Avatar asked Aug 04 '12 10:08

Abhishek Gupta


2 Answers

The command you're looking for is disown.

disown <processid>

This is as close as you can get to a nohup. It detaches the process from the current login and allows it to continue running. Thanks David Korn!

http://www2.research.att.com/~gsf/man/man1/disown.html

and I just found reptyr which lets you reparent a disowned process. https://github.com/nelhage/reptyr

It's already in the packages for ubuntu.

BUT if you haven't started the process yet and you're planning on doing this in the future then the way to go is screen and tmux. I prefer screen.

like image 113
jbrahy Avatar answered Sep 28 '22 08:09

jbrahy


You might also consider the screen command. It has the "restore my session" functionality. Admittedly I have never used it, and forgot about it.

Starting the process as a daemon, or with nohup might not do everything you want, in terms of re-capturing stdout/stdin.

There's a bunch of examples on the web. On google try, "unix screen command" and "unix screen tutorial":

  • http://www.thegeekstuff.com/2010/07/screen-command-examples/
  • GNU Screen: an introduction and beginner's tutorial
like image 26
Sunil D. Avatar answered Sep 28 '22 08:09

Sunil D.