Usage of nohup command in linux.

When applying Unix/Linux, we generally want a program to run in the background, so we will often use & at the end of the program to make 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 like mysqld, so we need the nohup command. How to use the nohup command? Here are some usages of the nohup command.

nohup /root/start.sh &

After pressing Enter in the shell, it prompts:

[~]$ 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 a role in the log. effect.

But sometimes there is a problem in this step. When the terminal is closed, the process will be closed automatically. Looking at nohup.out, you can see that the service is automatically closed when the terminal is closed.

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

When I showed it to me for the second time, I found out that I was different from him in one detail when operating the terminal: he needed to press any key on the keyboard on the terminal to return to the shell input command window after the nohup was successful in the shell prompt, and then Exit the terminal by entering exit in the shell; and I directly click the close program button to close the terminal after nohup is successfully executed. 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.

Some people didn't notice this detail like I did, so I recorded it here.

Attachment: nohup command reference

nohup command

Purpose : run the command without hanging up.

Syntax: nohup Command [ Arg … ] [ & ]

Description: The nohup command runs the command specified by the Command parameter and any associated Arg parameters, ignoring all SIGHUP signals. Use the nohup command to run programs in the background after logging out. To run the nohup command in the background, add & (symbol for "and") to the end of the command.

Whether or not the output of the nohup command is redirected to the terminal, the output is 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. The command specified by the Command parameter cannot be invoked if no file can be created or opened for appending. If standard error is a terminal, redirect all output of the specified command to standard error to the same file descriptor as standard output.

Exit Status: The command returns the following exit values:

126 The command specified by the Command parameter can be found but not invoked.

127 The nohup command encountered an error or could not find the command specified by the Command parameter.

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 the account, then 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 the command is: nohup command &

submit a job using the nohup command

If you submit a job using the nohup command, by default all output from the job is redirected to a file named nohup.out, unless an output file is specified otherwise:

nohup command > myout.file 2>&1 &

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

Use jobs to view jobs.

Use fg %n to close.

In addition, there are two commonly used ftp tools, ncftpget and ncftpput, which can realize ftp upload and download in the background, so that you can use these commands to upload and download files in the background. Usage of nohup command in linux.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326090103&siteId=291194637