18

I am a newbie in Ubuntu. I have learned a command

nohup [command] - run <command> immune to hangup signal

I can't understand the meaning of "immune to hangup signal". What type of signal it is talking about? And please tell me how to use this command with an example.

  • If available in your shell, disown is a better alternative to nohup. Background a command with command & or Ctrl-Z, then use disown to detach it from the shell. – John Kugelman Jan 13 '18 at 19:15
  • @JohnKugelman that is not sufficient, you have divert the input, output and error stream, too. So why not use 'nohup' that does this automatically for you? – miracle173 Jan 14 '18 at 10:59
34

Let us consider you have opened a gedit text editor from a terminal and working on it. If you close the terminal before closing gedit, the gedit also gets closed as soon as closing the terminal. So what is going on here? The gedit runs as a child process under the terminal. When you close the terminal a hang up signal (SIGHUP) is sent to the process which kills the child process.

On the other hand if you want your child process (here gedit) to keep on running even after closing the parent terminal, you would want your process immune to hangup signal. So that closing the terminal do not close the child process. nohup does exactly this job.

nohup does not disconnect a command from terminal, it makes your script ignore SIGHUP, and redirects stdout/stderr to a file nohup.out, so that the command can continue running in the background after you log out. If you close the shell/terminal or log off, your command is no longer a child of that shell. It belongs to init process. If you search in pstree you'll see it is now owned by process 1 (init). That cannot be brought back to the foreground because the foreground no longer exists.

16

Besides what @sourvac wrote, this is a legacy from Back In The Day when people logged in via text-only dumb terminals, often via (the original, analog 110 baud) modem: the "hang up" is literally "hanging up the phone".

A pid that's immune to hangup signal will keep on running even after you log out and hang up the phone. This was very useful on slow computers when jobs ran for hours upon hours, and you couldn't just stay logged in (someone else needed to use the terminal, you needed to use the phone, static in the line might "bzzt" the connection, etc).

  • 1
    Not just back in the day. I run most of my large jobs on a server cluster that I ssh into. If I didn't use nohup, I'd need to leave my laptop connected to those machines for days while waiting for the jobs to finish. – Ray Jan 13 '18 at 18:14
  • @Ray what about running them via "at" or as cron jobs? – RonJohn Jan 13 '18 at 18:46
  • @RonJohn Sure. Or via a grid engine. I didn't mean to suggest nohup was the only option, just that it is still used for this purpose today. – Ray Jan 13 '18 at 19:38
  • 1
    Maybe your historical remarks are correct. But your conclusions about its present importance are wrong. It is not useful on slow computers but it is useful for long running jobs. These are two different things. You use use 'nohup' to protect your job against the intended or unintended disconnection of your terminal. Even nowadays you are not happy if a jobs aborts because a network error disconnects your terminal and your job is aborted. – miracle173 Jan 14 '18 at 10:49
  • @Ray Cron jobs are completely inappropriate to achieve this. Cronjobs are for periodic tasks, you have to set the environment in the cronjob and consider lot of other things. Maybe 'at' is a little bit easier to handle but nevertheless more complex than nohup. – miracle173 Jan 14 '18 at 10:50

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.