What is nohup?
nohup (short for "no hang up") is a Linux command used to run a command or script immune to hangups, meaning it continues running even after the terminal is closed.
Syntax:
nohup command [arguments] &
🔸 What Does nohup Do Internally?
Ignores the SIGHUP (signal 1), which is typically sent to a process when its controlling terminal closes.
Redirects stdout to nohup.out and stderr to nohup.out (if not redirected).
Places the process in the background (when & is used).
Example:
nohup ./myscript.sh > output.log 2>&1 &
🔸 Related or Similar Commands
🔸 Edge Cases and Considerations
Terminal Closes Mid-Execution
Without nohup, command dies.
With nohup, continues executing.
Redirection Matters
If stdout or stderr isn’t redirected, it goes to nohup.out.
Important for logging output.
Signal Ignoring Is Limited
Only ignores SIGHUP, not SIGKILL or SIGTERM.
nohup Doesn’t Auto Background
You must append & or it runs in the foreground.
Does Not Create a New Session
Use setsid if you need session detachment.
Zombie Process Risk
If parent dies and nohup is not used with proper reparenting, process might become orphan or zombie.
TTY Dependencies
If command needs TTY interaction (e.g., vi), nohup breaks it.
Job Control Confusion
Use jobs, fg, bg, and disown carefully in conjunction with nohup.
🔥 20 FAANG-Level Questions and Answers on nohup and Related Commands
🔹 Basics & Internals
1. What problem does nohup solve?
It prevents a process from receiving the SIGHUP signal and dying when the terminal closes.
2. How does nohup differ from using & alone?
& runs in the background, but the process still dies on terminal exit. nohup ignores SIGHUP.
3. What is nohup.out? When is it created?
Default output file for stdout/stderr if redirection is not specified.
4. Why might a nohup command still terminate on logout?
If it's tied to a shell job, and disown or setsid wasn't used.
5. What signal does nohup ignore, and how?
Ignores SIGHUP using signal(SIGHUP, SIG_IGN).
🔹 Practical Scenarios
6. How do you make a script run after terminal logout and not write to nohup.out?
nohup ./myscript.sh > /dev/null 2>&1 &
7. Can nohup prevent SIGKILL or kill -9?
No. SIGKILL cannot be trapped or ignored.
8. How to view the PID of a nohup background job?
nohup ./script.sh & echo $!
9. How do you monitor if a nohup job is still running?
ps -ef | grep myscript
10. What’s the difference between nohup, setsid, and disown?
nohup: ignores SIGHUP
setsid: runs in new session
disown: detaches job from shell job table
🔹 Advanced Debugging & Design
11. Why does nohup not detach from the terminal fully?
It still shares session and process group unless used with setsid.
12. How can nohup be misused in a CI/CD or production environment?
Logs grow uncontrollably in nohup.out. Lack of monitoring/control.
13. What happens if the parent process exits and child was run with nohup but not disown?
Child might still get SIGHUP if the shell forwards it. Use disown.
14. How to fully daemonize a process without external tools?
Use fork(), setsid(), umask(), chdir(), close FDs manually.
15. Compare nohup vs systemd for background processes.
nohup: ad-hoc, lacks monitoring. systemd: full-featured service control.
🔹 Real-World Usage
16. How would you use nohup in a data pipeline that takes hours?
nohup python transform.py > logs/output.log 2>&1 &
17. You run a job with nohup, but output isn't visible in nohup.out. Why?
The command might buffer output, or stdout was redirected elsewhere.
18. How do you gracefully stop a nohup process?
kill -TERM <pid>
19. What's the danger of using nohup without logging in production?
Silent failures, no error traceability, uncontrolled file growth.
20. Design a resilient long-running service using nohup, log rotation, and cron.
Use nohup ./service.sh >> /var/log/service.log 2>&1 &
Rotate logs via logrotate
Cron monitor:
*/10 * * * * pgrep service.sh || nohup ./service.sh &
Let me know if you want a PDF handout, diagram of process groups/sessions, or a visual cheatsheet for nohup and alternatives like tmux, screen, and disown.