What is the difference between running a command with & and with nohup?
& only puts the command in the background of the current shell; it can still die when the terminal closes. nohup makes the command ignore the hangup signal, so it keeps running after logout, and sends its output to nohup.out unless redirected.
Explain it simply
Picture a student sitting at a library table. & means he keeps studying while you chat, but he is still there on your invitation: if you leave and the library closes, he gets sent out too. nohup means he has been told "do not leave even if the hall closes".
When you close a terminal or your SSH session drops, the shell sends SIGHUP to its jobs. Most programs exit on it. nohup starts the command with SIGHUP ignored, so it survives.
Because the terminal disappears, nohup also redirects output to a file called nohup.out if you do not choose one. Usually you write it yourself and combine both tools:
nohup ./train.sh > train.log 2>&1 &
For an already running job, use disown -h %1 instead. For long interactive work, tmux or screen are better. For real services, write a systemd unit.
Example
nohup python3 report.py > report.log 2>&1 &
# [1] 4821
tail -f report.logWhat to say in the interview
"& backgrounds a job but it can still receive SIGHUP when the session ends. nohup ignores SIGHUP so the job survives logout. I usually write nohup cmd > log 2>&1 & and, for permanent services, use systemd."
Common mistakes
- Using nohup without & and then being unable to use the terminal.