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:
#!/bin/sh
command_1
script2.sh
# Starting ignoring HUP signal
trap "" HUP
script3.sh
...
(tests & loops, etc)
script4.sh
# Resume normal HUP handling.
trap - HUP
script5.sh
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.