What's nohup?
The commandnohup(abbreviation of «no hang up») is an essential tool in Unix-type systems that allows you to run a process to ignore the SIghUP signal, which is sent to the processes when the terminal session is closed. Without nohup, by closing the session or disconnecting by SSH, the child process would receive SIghup and be abruptly completed, which can cause data loss or interruption of critical tasks. With nohup, the process continues to run in the background, and its standard output and error are by default redirected to a file callednohup.outin the current directory, unless another destination is specified. This feature makes nohup ideal for backup scripts, long compilations, system updates or any task that requires to remain active after the user leaves the terminal.
Basic syntax
The simplest way to usenohupis:
nohup command [arguments] &
The ampersand (&) in the end sends the process to the background after nohup has launched it. If the & is not included, the process will be run in the foreground and the terminal will be blocked until it is finished, although it will continue to ignore SIghUP. In addition, it is possible to redirect the standard output and error explicitly:
nohup command > salida.log 2 > error.log &
In this way, the administrator can control where the records are kept and prevent the filenohup.outgrow up without control. It is also possible to combinenohupwithniceorioniceto adjust the CPU or I / O priority of the process.
Examples of use
- Running a night back script:
nohup / usr / local / scripts / backup.sh > / var / log / backup _ nohup.out 2 > & 1 &
- Compilation of a large project:
nohup make -j$(nproc) > build.log 2 > & 1 &
- Run a development server in the background:
nohup python3 manage.py runserver 0.0.0.0: 8000 > server.log 2 > & 1 &
- Remote package update using SSH:
nohup apt-get update & apt-get upgrade - and > / tmp / upgrade.log 2 > & 1 &
In each case, the process continues even if the SSH session is closed, and the records are saved in the specified files for further review.
Differences with & and disown
The operator&only places a job in the background, but does not protect it from the SIghUP signal; if the terminal is closed, the work will receive the signal and will be finished unless it has been disconnected previously. The commanddisown, available in shells such as Bash and Zsh, removes work from the shell's work table, preventing the shell from taking it into account and, therefore, sending SIghUP to you when it is closed. However,disownmust be executed after the process has been initiated, and does not automatically redirect the output. Instead,nohupacts in a preventive way: before launching the process, it tells you to ignore SIghup and, by default, redirect your exit tonohup.out. So,nohupis the most direct option when you need to ensure the continuity of a process from the moment of its launch.
&→ background, vulnerable to SIghup.disown→ removes from the job control, but requires further action.nohup→ Ignore SIghup from the start and manage the output.
Good practices
- Review the file regularly
nohup.outor custom logs to detect errors or unexpected messages. - Use log rotation (e.g. with
logrotate) if the process generates a lot of output, preventing the disk from filling. - Combine
nohupwith multiplexion tools likescreenortmuxwhen occasional interaction with the process is needed. - Avoid running commands that require interactive low input
nohup, since they can't read from the terminal and they could be blocked. - When you need to change the process priority, use
niceorionicebeforenohupfor example:nice -n 10 nohup comando &.
Conclusion
The commandnohupis a simple and reliable solution to keep processes running after closing the terminal or disconnecting from an SSH session. By making the process ignore the SIghup signal and redirect its output to a log file,nohupprevents unexpected interruptions and facilitates the administration of long or critical tasks. Knowing your syntax, understanding how you differ from simple background operators and applying good registration practices and priority allows any system manager or developer to make the most of this tool in Linux environments.


