Sign up ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

How can run a part of a shell script in nohup mode ? My script goes like this:


(tests & loops, etc)

and what I would like is to run the part from to in nohup mode without using the command 'nohup' or '&' so if the user get disconnected, the script execution continues.

Dont' know if my question is clear enough :) Thank you!

share|improve this question
Your question is not clear. You want the functionality provided by nohup, but you don't want to use nohup. Why? –  ThatGraemeGuy Aug 21 '12 at 13:10
sorry about this confusing question! from to there's about 100 lines of code (loops, conditions, etc), I would like to avoid putting & at the end of each line, I would like to wait until the previous command has finished before executing the current line, and finally I dont know how to execute nohup on structure like loops or conditions. –  PapelPincel Aug 21 '12 at 13:32
why the -1 ??! a question is a question, a least two people understood it, answered and let me learn something new (hope I won't be the only one to learn new stuff here) –  PapelPincel Aug 21 '12 at 15:12

2 Answers 2

up vote 8 down vote accepted

The primary purpose of nohup is to insulate programs from the HUP signal, typically received when the controlling TTY is disconnected (e.g., because of the user logging out).

You can accomplish the same thing using the shell builtin command trap:

$ help trap
Trap signals and other events.

Defines and activates handlers to be run when the shell receives signals
or other conditions.

ARG is a command to be read and executed when the shell receives the
signal(s) SIGNAL_SPEC.  If ARG is absent (and a single SIGNAL_SPEC
is supplied) or `-', each specified signal is reset to its original
value.  If ARG is the null string each SIGNAL_SPEC is ignored by the
shell and by the commands it invokes.

So you can use the trap command to insulate portions of your script from the HUP signal by passing a null string as the action. For example:



# Starting ignoring HUP signal
trap "" HUP
(tests & loops, etc)

# Resume normal HUP handling.
trap - HUP

You will probably want to ensure that output to your script is being directed to a file (otherwise you will lose any output after you disconnect, even if the script keeps running).

You might also want to consider simply running the script under control of screen, since this has the advantage that you can re-attach to the running script after getting disconnected.

share|improve this answer

In general it is bad practice to exit a script while it still has processes running in the background.

Apply the KISS principle, or "Keep it Simple, Sam" (for various values of Sam). You understand nohup, just use it.

If you want through and all conditionals and loops in between to run to completion after your script returns to the shell prompt, put that section of the script in a separate file.

You now get two scripts. First a script


nohup &

#! /bin/bash
(tests & loops, etc)

If you want to start the script from cron, why bother? If you start from a shell session, follow @larsks' advice, and use a terminal emulator/multiplexer like screen or tmux.

share|improve this answer
Thank you Henk. I first tought about moving all the part into a separate script file but I wanted to keep it all in one place. I'm running the script from shell session so screen or tmux would be a good alternative. –  PapelPincel Aug 21 '12 at 14:27

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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