Linux Shell nohup command usage (code demonstration included)

1. Usage of Linux Shell nohup command

When using Unix/Linux, we generally want a program to run in the background, so we will often use & at the end of the program to let the program run automatically. For example, we want to run mysql in the background: /usr/local/mysql/bin/mysqld_safe -user=mysql &. But there are many programs that do not want mysqld, so we need nohup command, how to use nohup command? Here are some usages of the nohup command.

nohup /root/start.sh &

Prompt after pressing Enter in the shell:

[~]$ appending output to nohup.out

The standard output of the original program is automatically redirected to the nohup.out file in the current directory, which plays the role of log.

But sometimes there will be problems in this step. When the terminal is closed, the process will be automatically closed. Check nohup.out and you can see that the service is automatically closed when the terminal is closed.

After consulting the Red Flag Linux engineer, he was also puzzled. After executing it on my terminal, the process he started was still running after closing the terminal.

In the second demonstration, I found that I was different from his operation in the terminal in one detail: he needed to press any key on the terminal keyboard to return to the shell input command window after the successful nohup prompt in the shell, and then Exit the terminal by typing exit in the shell; and I click the close program button to close the terminal every time nohup is executed successfully. Therefore, the session corresponding to the command will be disconnected at this time, causing the process corresponding to nohup to be notified that it needs to be shut down together.

Someone like me didn't notice this detail, so I recorded it here.

Attachment: nohup command reference

nohup 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. Use the nohup command to run programs in the background after logging off. To run the nohup command in the background, add & (the symbol for "and") to the end of the command.

Regardless of whether the output of the nohup command is redirected to the terminal, the output will be appended to the nohup.out file in the current directory. If the nohup.out file in the current directory is not writable, the output is redirected to the $HOME/nohup.out file. If no file can be created or opened for appending, the command specified by the Command parameter cannot be called. If standard error is a terminal, all output written to standard error of the specified command is redirected to the same file descriptor as standard output.

Exit status: This command returns the following exit values:

126 The command specified by the Command parameter can be found but cannot be called.

127 An error occurred in the nohup command or the command specified by the Command parameter could not be found.

Otherwise, the exit status of the nohup command is the exit status of the command specified by the Command parameter.

nohup command and its output file

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. Nohup means not to hang up (n ohang up).

The general form of this command is: nohup command &

Submit the job using the nohup command

If you use the nohup command to submit a job, all output of the job is redirected to a file named nohup.out by default, unless you specify an output file:

nohup command > myout.file 2>&1 &

In the above example, the output is redirected to the myout.file file.

Use jobs to view tasks.

Use fg %n to close.

In addition, there are two commonly used ftp tools, ncftpget and ncftpput, which can implement ftp upload and download in the background, so that these commands can be used to upload and download files in the background.

Second, the Linux nohup command in detail

nohup command and its output file

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 I need to use the nohup command.
The perfect solution: nohup ./start-dishi.sh >output 2>&1 &
now explain the above command

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

There are three commonly used streams in operating systems:

  • 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.

Step into the main topic below:

nohup ./start-dishi.sh >output 2>&1 &

Explanation:

  1. The command line with &, even if the terminal is closed or the computer crashes, the program still runs (provided that you submit the program to the server);
  2. The meaning of 2>&1    means to
      redirect standard error (2) to standard output (1), and standard output is imported into file output, so the result is that both standard error and standard output are imported into file output. As for why it is necessary to redirect standard error to standard output, it comes down to the fact that standard error has no buffer, but stdout has. This will cause the >output 2>output file output to be opened twice, and stdout and stderr will compete for coverage, which is definitely not what we want.
      This is why someone would write: nohup ./command.sh >output 2 >The reason for the output error
    =========================================== ===============================================
    Last talk/ 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 when you don’t care, you can use stdout and stderr to be directed here>./command.sh >/dev/null 2>&1

What are you waiting for? I recommend my linuxC/C++ language exchange group: [ 1106675687 ] I have compiled some learning books and video materials that I think are better to share in the group files, and you can add them if you need them! The top 100 enter the group to receive, an extra copy of C/C++, linux materials worth 199 included (video tutorials, e-books, actual combat projects and codes)
Insert picture description here
Insert picture description here

Guess you like

Origin blog.csdn.net/m0_50662680/article/details/112669007