Abhishek Gupta
Abhishek Gupta

Reputation: 1193

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.

Upvotes: 8

Views: 13880

Answers (4)

YePhIcK
YePhIcK

Reputation: 5866

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

Upvotes: 4

jbrahy
jbrahy

Reputation: 4425

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.

Upvotes: 14

Norman Gray
Norman Gray

Reputation: 12514

'Interact with' can mean a couple of things.

The reason why a program, started at the command-line, exits when the terminal ends, is because the shell, when it exits, sends that process a HUP signal (see documentation for kill(1) for some introduction; HUP, by the way, is short for 'hang up', and originally indicated that the user had hung up the modem/telephone). The default response to a HUP signal is that a process is terminated – that is, the invoked program exits.

The details are slightly more fiddly, but this is the general intuition.

The nohup command tells the shell to start the program, and to do so in a way that this HUP signal is ignored. That is, the program keeps going after the invoking terminal exits.

You can still interact with this program by sending it signals (see kill(1) again), but this is a very limited sort of interaction, and depends on your program being written to do sensible things when it receives those signals (signals USR1 and USR2 are useful things to trap, if you're into that sort of thing). Alternatively, you can interact via named pipes, or semaphores, or other bits of inter-process communication (IPC). That gets fiddly pretty quickly.

I suspect what you're after, though, is being able to reattach a terminal to the process. That's a rather more complicated process, and applications like screen do suitably complicated things behind the scenes to make that happen.

The nohup thing is a sort of quick-and-dirty daemonisation. The daemon(3) function does the daemonisation 'properly', doing various bits of tidy-up as described in YePhIcK's answer, to comprehensively break the link with the process/terminal that invoked it. You can interact with that daemonised process with the same IPC tools as above, but not straightforwardly with a terminal.

Upvotes: 0

Sunil D.
Sunil D.

Reputation: 18193

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":

Upvotes: 6

Related Questions