Home  >  Article  >  php教程  >  Use the nohup command to run Linux programs in the background

Use the nohup command to run Linux programs in the background

高洛峰
高洛峰Original
2016-11-23 11:51:092185browse

Use nohup to make the program always run in the background

Under Unix/Linux, if you want a certain program to run in the background, many people 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 many of our programs are not made into daemons like mysqld. Maybe our programs are just ordinary programs. , generally this kind of program ends with &, but if the terminal is closed, the program will also be closed. But in order to be able to run in the background, we can use the nohup command. For example, if we have test.php that needs to be run in the background, and we want it to run regularly in the background, then use nohup:

nohup /root/test.php &
 Tips:

 [~]$ appending output to nohup.out

 Well, it proves that the operation is successful, and at the same time, the output information of the program operation is placed in the nohup.out file in the current directory.

Attached: 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 related Arg parameters, ignoring all hangup (SIGHUP) signals. Use the nohup command to run programs in the background after logging out. To run a 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, 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 is not invoked. If standard error is a terminal, then all output of the specified command written to standard error is redirected to the same file descriptor as standard output.

Exit status: The command returns the following exit value:

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 cannot 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 &

Use the nohup command to submit a job

If you use the nohup command to submit a job, then by default all output of the job will be redirected to a file called nohup.out file, 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 tasks.

Use fg %n to close.

There are also two commonly used ftp tools ncftpget and ncftpput, which can realize ftp upload and download in the background, so I can use these commands to upload and download files in the background.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn