nohup - Linux
Overview
nohup, short for “no hangup”, is a POSIX command used to run another command with hangup signals ignored, enabling the command to continue running in the background even after the user has logged out. This command is particularly useful for long-running processes in remote servers and batch jobs that need to continue running regardless of the user’s session status.
Syntax
The basic syntax of nohup is as follows:
nohup <command> [arguments] &
<command>: This is the command you want to run without hangup.[arguments]: These are optional arguments for the command.&: When appended at the end, this symbol sends the command to run in the background.
Options/Flags
nohup has a very limited set of options:
--help: Display a help message and exit.--version: Output version information and exit.
Because nohup is a straightforward utility, most of its functionality revolves around the command it is paired with rather than options within nohup itself.
Examples
-
Basic Example
Running a Python script in the background:
nohup python myscript.py & -
Output Redirection
Redirecting output to a file other than
nohup.out:nohup ./my_script.sh > my_output.log 2>&1 &Here,
> my_output.logredirects standard output tomy_output.log,2>&1redirects standard error to standard output, ensuring all output goes to the same file. -
Combining with
tailto Monitor Outputnohup ./long_running_process.sh > process.log 2>&1 & tail -f process.logThis approach allows you to start a process in the background but immediately start watching its output using
tail.
Common Issues
-
Output Issues: If
nohupis invoked without redirecting the output, it writes the output to a file namednohup.outin the current directory. If it cannot write to this file, it attempts to write tonohup.outin the user’s home directory. If it cannot write there, the command fails. -
Ignoring Input:
nohupignores all input (stdinis redirected from/dev/null). Any command that requires user input after invocation will not work correctly when called withnohup.
Integration
nohup is often combined with other terminal commands to achieve uninterrupted server processes:
-
Script Execution at Boot-time
You can include
nohupin a startup script to ensure that specific commands persist after the boot process:nohup /path/to/script.sh & -
Cron Jobs
Although
cronjobs do not typically requirenohup, in complex scripts where subprocesses might hang up due to user logouts, wrapping the entire cron job or specific commands innohupcan prevent unintended exits.
Related Commands
&: Used to run any command in the background in Linux.disown: Removes jobs from the shell’s job table, making them immune to hangup signals.screenortmux: More advanced tools that allow for detaching and reattaching to shell sessions.
For further reading and additional details, you might visit the official GNU coreutils page or consult the man pages by typing man nohup in your terminal.