Differences between "<command> & disown" and "nohup <command> & disown"

This is my understanding about the usage of &, disown and nohup:

  • <command>: runs the process within the Terminal's current bash instance, in the foreground (i.e. the process is listed as a bash foreground job and stdin, stdout and stderr are still bound to the terminal); not immune to hangups;
  • <command> &: runs the process within the Terminal's current bash instance, in the background (i.e. the process is listed as a bash background job and stdin, stdout and stderr are still bound to the terminal); not immune to hangups;
  • <command> & disown: runs the process within the Terminal's current bash instance, in the background, but the process is detached from the bash's jobs' list (i.e. the process is not listed as a bash foreground / background job and stdin, stdout and stderr are still bound to the terminal); immune to hangups;
  • nohup <command> & disown: runs the process within the Terminal's current bash instance, in the background, but the process is detached from the bash's jobs' list (i.e. the process is not listed as a bash foreground / background job and stdin, stdout and stderr are not still bound to the terminal);immune to hangups;

So, aside from nohup <command> & disown blocking stdin and redirecting stdout and stderr to nohup.out by default, it seems to me like it can be considered totally equivalent to <command> & disown.

Is the above all correct? Any misconception?


Solution 1:

Your understanding is basically correct. Both disown and nohup are used to allow you to exit a running shell session without stopping running jobs. Some clarifications:

  • There's no reason to run nohup command & disown, nohup will already disown it for you.

  • nohup is defined by POSIX while disown is not. This means that while many shells (e.g. bash, zsh, ksh) have it, others (for example tcsh, csh, dash and sh) won't have it.

  • disown can be used after a command has been launched while nohup must be used before.

As far as I can tell, the actual effect of the two commands is the same. They each have features that the other lacks (see help disown and man nohup) but their basic function is the same, yes.

For a far more detailed discussion of these tools and the differences between them, have a look at the answers here:

  • Do disown -h and nohup work effectively the same?
  • Difference between nohup, disown and &

Solution 2:

Your first to third points seem alright, although stdin, stdout and stderr are still bound to the terminal is not a right notion. stdin is always bound to terminal in the sense that you would always input the file name to a command via terminal or ways using terminal. stdout and stderr are still bound to the terminal is alright.

Your have stdin, stdout and stderr are not still bound to the terminal in the fourth point which is not right as mentioned in my earlier paragraph. Also here you are using /dev/null as the input file to the command, for example if the command is cat, you are using it as cat /dev/null.

The command on your 5th point is not correctly put, you used nohup <command> & disown, while using any one of nohup or disown the other one is not needed. Their purposes are the same (to ignore the SIGHUP) but they function in a bit different way. So the command can be simplified as nohup <command> &.