What would be the best method of using nohup in a batch script with multiple calls inside the script.

For example, I have a script called filebuilder.sh and it contains something like:

/usr/bin/python buildFile.py fileA
/usr/bin/python buildFile.py fileB
/usr/bin/python buildFile.py fileC

What I want to do is make ensure that if I run my script, it will continue even if my SSH session is terminated. Should I just call the script like so:

nohup filebuilder.sh

or place nohup before every entry in the file such as:

nohup /usr/bin/python buildFile.py fileA
nohup /usr/bin/python buildFile.py fileB
nohup /usr/bin/python buildFile.py fileC 

or Both? If I place it only on the script, is it possible that one of the python calls could still fail on a termination signal? What is the best practice and which solution will accomplish my requirements? Is there a better alternative?

up vote 0 down vote accepted

Putting nohup on each line is not the answer.  If you do that, and the session is terminated, the currently-running program will run to completion, but then the script will terminate without finishing.  nohup filebuilder.sh will work, but then you have to remember to do it every time.  A couple of solutions:

  1. Write filebuilder.sh to say

    exec nohup name_of_the_real_filebuilder.sh

  2. Put

    trap "" 1

at the beginning of your script.  (1 is the numeric value of the “hangup” signal.)

  • thanks for the response. Is that a do both situation or an either or situation for your solutions? – Dave Johnshon Oct 24 '12 at 3:08
  • @Dave: Either (1) or (2). – Scott Oct 24 '12 at 17:06

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.