How can run a part of a shell script in nohup mode ? My script goes like this:
#!/bin/bash
command_1
script2.sh
script3.sh
...
(tests & loops, etc)
script4.sh
script5.sh
and what I would like is to run the part from script3.sh to script5.sh 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!
Best Answer
The primary purpose of
nohup
is to insulate programs from theHUP
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
:So you can use the
trap
command to insulate portions of your script from theHUP
signal by passing a null string as the action. For example: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.