MULTIPLE COMMANDS WITH IN A NOHUP
###################################

Nohup is useful to run things in the background tied away from the session shell (putty or teraterm or etc.). So that if you close the putty session, your commands will still run. Also you can move on to doing other things while your commands are being run.

Without Nohup, and only using the & background ampersand, your process will run in the bg (background), but if you close the putty/shell session that bg process will die with it. So nohup fixes that by untie-ing everything from the putty shell and tying them to the init process (which is tied to the initial process of the system). So your process will only die if you kill it or if you reboot the system (or kill init)

Here is how you run nohup with multiple commands. You just wrap it in a sh -c. Every shell session has access to “sh -c” (which is usually dash or busybox). I wouldnt recommend wrapping in bash (although can do it), because some shells might not have bash, but all have sh.

NORMAL NOHUP – output saved to nohup.out

NOTE: the parenthesis just launch a subshell which has no effect on the output here. IF your using external variables and internal variables then they might have an effect.

SAVE OUTPUT TO outputfile

SAVE ERROR and OUTPUT TO outputfile

SAVE ERROR and OUTPUT TO outputfile (bash 4)

EXAMPLE 1: Measure the size of current dir (with human readable and then with none-human-readable, so in Kilobytes) and save the output to /root/sizes2.txt. In the mean time you can do whatever you want and it will run.

EXAMPLE 2: Same example but using parenthesis

 

Leave a Reply

Your email address will not be published. Required fields are marked *