Q:

How to detach a process from terminal in 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. 

A:

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. 

A:

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

A:

First google result for "UNIX demonizing a process":
See the daemon(3) manpage for a short overview. The main thing of daemonizing 
is going into the background without quiting or holding anything up. A list of 
things a process can do to achieve this:

fork() 
setsid() 
close/redirect stdin/stdout/stderr to /dev/null, and/or ignore SIGHUP/SIGPIPE. 
chdir() to /.

If started as a root process, you also want to do the things you need to be root 
for first, and then drop privileges. That is, change effective user to the "daemon" 
user or "nobody" with setuid()/setgid(). If you can't drop all privileges and need 
root access sometimes, use seteuid() to temporary drop it when not needed.
If you're forking a daemon then also setup child handlers and, if calling exec, 
set the close on exec flags on all file descriptors your children won't need.
And here's a HOWTO on creating Unix daemons: http://www.netzmafia.de/skripten/unix/linux-daemon-howto.html

