Detailed Linux-nohup command

Scenes

Deploy the wdt program on Linux today and execute ./start-dishi.sh on the SSH client. The startup is successful. After the SSH client is closed, the running program is also terminated. How can I ensure that the program will work after the SSH client is launched? Is it always executed? Searching for information on the Internet, I found that the nohup command is needed.

The perfect solution: nohup ./start-dishi.sh >output 2>&1 &

command

Purpose: Run commands without hanging up.
Syntax: nohup Command [Arg…] [&]
Description: The nohup command runs the command specified by the Command parameter and any related Arg parameters, ignoring all hang-up (SIGHUP) signals. After logging off, use the nohup command to run the program in the background. To run the nohup command in the background, add & (the symbol for "and") to the end of the command.

nohup is the abbreviation of no hang up, which means not to hang up.

nohup command: If you are running a process, and you feel that the process will not end when you log out of your account, you can use the nohup command. This command can continue to run the corresponding process after you log out of the account/close the terminal.

By default, all output of the job is redirected to a file named nohup.out.

There are three commonly used streams in the operating system:
  0: standard input stream stdin
  1: standard output stream stdout
  2: standard error stream stderr

Generally, when we use> console.txt, it is actually the omitted usage of 1>console.txt; <console.txt is actually the omitted usage of 0 <console.txt.

Example

  1. **nohup ./command > myout.file 2>&1 & **

In the above example, 0 – stdin (standard input), 1 – stdout (standard output), 2 – stderr (standard error);

2>&1 redirects the standard error (2) to the standard output (&1), and then the standard output (&1) is redirected to the myout.file file.

  1. nohup ./command.sh >/dev/null 2>&1 &

The function of the /dev/null file is a bottomless pit. Anything can be directed here, but it cannot be opened. So generally large stdou and stderr can be directed here when you don’t care about stdout and stderr

The difference between nohup and &

&: Refers to running in the background, but when the user exits (suspends), the command automatically exits

nohup: Run without hanging up. Note that there is no background running function, which means that running a command with nohup can make the command execute permanently, and it has nothing to do with the user terminal. For example, disconnecting the SSH connection will not affect his Run, note that nohup does not mean running in the background; & is running in the background

So, we can cleverly combine them to use
nohup COMMAND &
so that the command can be executed permanently in the background

E.g:

  1. sh test.sh &

Put the sh test.sh task in the background, close xshell, and stop the corresponding task.

  1. nohup sh test.sh
    puts the sh test.sh task in the background, closes the standard input, the terminal can no longer receive any input (standard input) , redirects standard output and standard error to the nohup.out file in the current directory, even if it is closed xshell exits the current session and continues to run.
  2. nohup sh test.sh &
    put the sh test.sh task in the background, but standard input can still be used, the terminal can receive any input , redirect standard output and standard error to the nohup.out file in the current directory, even if xshell is closed to exit The current session continues to run.

Conclusion

Welcome to follow the WeChat public account "Zone ZONE", focusing on sharing Java and cloud computing related content, including SpringBoot, SpringCloud, microservices, Docker, Kubernetes, Python and other related technical dry goods, looking forward to meeting you!
Insert picture description here

Guess you like

Origin blog.csdn.net/feifuzeng/article/details/108192818