nohup command tutorial in linux/unix with examples and use cases

linux nohup command – run a command immune to hangups, with output to a non-tty

When the linux nohup command is used to run jobs (scripts) in the background, it can ignore the hangup signal and cooperate with output redirection to achieve long-term jobs (scripts) run in the background.

Plays a very important role in daily development.

Syntax

nohup COMMAND [ARG]...

Options

  • –help
    display help and exit
  • –version
    output version information and exit

Examples & Use Cases

Run script in background

1.In terminal window 1, run the script in the background using the linux nohup command and then close this terminal window.

➜  nohup ./test.sh > /dev/null 2>&1 &
[1] 7799
➜  exit

2.Open a new terminal window 2 and use the linux ps command to query the script process.

➜  ~ ps -ef | grep "test.sh" | grep -v grep
ylspirit   7799      1  0 17:09 ?        00:00:00 /bin/bash ./test.sh
➜  ~

  • 2> &1:
    Redirects standard error to standard output. Here our standard output has been redirected to the /dev/null empty device, so standard error is also output to /dev/null.
  • & :
    To let the command execute in the background.

Add a Comment

Your email address will not be published.