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
# if you dont know your current shell just use "sh" nohup sh -c 'command1; command2;' & nohup sh -c '(command1; command2;)' & # you can find out your current shell like this: echo $SHELL # find out if it actually points to another file, i.e. find out if its a symlink to another file: ls -lisah `echo $SHELL` # if it points to /bin/sh (simpler version of BASH, called DASH or busybox - might not support parenthesis/subshells) nohup sh -c 'command1; command2;' & nohup /bin/sh -c 'command1; command2;' & # if it points to /bin/bash (bash it self, has more complex ommands, does support parenthesis/subshells): nohup sh -c 'command1; command2;' & nohup sh -c '(command1; command2;)' & nohup /bin/bash -c 'command1; command2;' & nohup /bin/bash -c '(command1; command2;)' & # Note: the next example will only show with "sh" however realize that you can use "/bin/sh" or "/bin/bash" as well.
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
nohup sh -c 'command1; command2;' > outputfile & nohup sh -c '(command1; command2;)' > outputfile &
SAVE ERROR and OUTPUT TO outputfile
nohup sh -c 'command1; command2;' > outputfile 2>&1 & nohup sh -c '(command1; command2;)' > outputfile 2>&1 &
SAVE ERROR and OUTPUT TO outputfile (bash 4)
nohup sh -c 'command1; command2;' &> outputfile & nohup sh -c '(command1; command2;)' &> outputfile &
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.
nohup sh -c 'echo "-----[$(date +s%s_d%Y-%m-%d_t%H-%M-%S)][human]------"; du -ch --max-depth 1; echo "-----[$(date +s%s_d%Y-%m-%d_t%H-%M-%S)][kb]------"; du -c --max-depth 1' > /root/sizes2.txt &
EXAMPLE 2: Same example but using parenthesis
nohup sh -c '(echo "-----[$(date +s%s_d%Y-%m-%d_t%H-%M-%S)][human]------"; du -ch --max-depth 1; echo "-----[$(date +s%s_d%Y-%m-%d_t%H-%M-%S)][kb]------"; du -c --max-depth 1)' > /root/sizes1.txt &