nohup runs php script _php instance _ script home in the background

nohup runs php scripts permanently in the background

Updated: March 23, 2024 11:09:59 Submitted by: yin
linux run a program, if we exit the terminal, or the network is not good connection interrupt, then the program will be suspended, and this situation is certainly not what we want to see, to let the PHP program in Linux background execution, you can use the nohup command with the & symbol,nohup command can make the process continue to run after the user logout, &can The process is executed in the background

What is a nohup

nohup is a command in Linux and Unix that allows a process to continue running in the background when the terminal exits. Its full name is "no hang up", which means "not to hang up". The nohup command allows you to continue running the command after exiting the terminal or closing the SSH connection.

nohup syntax rules

The basic syntax of the nohup command is as follows:

1

nohup COMMAND [ARGS ...] [> output-file 2> error-file] &

The meanings of the parameters are as follows:

  • COMMAND: Commands or scripts that need to be run in the background.
  • ARGS: Specifies the parameters of a command or script.
  • > output-file: Output redirects to the specified file.
  • 2> error-file: Error messages are redirected to the specified file.
  • &: Puts the command in the background.

The nohup command is executed in the following steps:

  • The nohup command redirects the current shell's standard input, standard output, and standard error output to/dev/nullIn the device, avoid being interrupted by the signal that shuts down the terminal.
  • The nohup command executes the process in the background and outputs the PID of the process to the terminal.
  • The process starts to execute and redirects the standard output and standard error output to the specified file.
  • The user can exit the terminal or close the terminal window, and the process still runs in the background.

At work, we often run a very important program, sometimes this program needs to run for several hours, or even several days, at this time if we exit the terminal, or the network is not good and the connection is interrupted, then the program will be suspended. And this is definitely not what we want to see, we want to even if the terminal is closed, the program can still run.

To make a PHP program execute in the Linux background, use the nohup command with the & symbol. The nohup command allows the process to continue running after the user has logged out, & you can put the process in the background to execute.

The nohup command is an abbreviation of the English phrase no hangup, which means not to hangup, meaning that the program does not exit. This command will cause the program to ignore the HUP signal and ensure that the program can proceed normally. The HUP signal, which some people may be unfamiliar with, is a signal sent to the process associated with it when the terminal is suspended, and the process will stop running after receiving this signal. So if you don't want progress to be killed by this signal, you can ignore it. And that's what nohup ordered to do.

php programs run in the linux background

Command: nohup php socket.php &

In this way, the terminal will not be disconnected and a nohup.out file will be generated in the project directory, recording all information and exceptions to close using:

ps aux | grep php netstat -anp | grep (port number)

Look at the PID and use: kill (PID)

1. Use nohup to run php scripts permanently in the background:

nohup php -f /www/wwwroot/default/redis_subscribe.php &

2. Check the process:

ps -ef|grep redis_subscribe.php

3. View all processes:

ps -ef

4. View a process:

ps [PID]

5. Terminate the process:

kill -9 [PID]

Common method of the nohup command

Start a program using the nohup command

If you need to run a program that keeps running even after the corresponding Shell is exited, you can run the program using nohup like this:

$ nohup command

When the program is running, the log output of the program and its error log will be recorded in the nohup.out file, which is usually located in the home directory or the current directory.

Redirects the program's output

What if I don't want to save the output of the program in my home directory or in the current directory, I want to save it in the path I specify, and I want to customize the file name? At this point we can use the redirect action >.

For example, I now have a script called myScript.sh and I want to save its output in the output directory of my home directory called myOutput.txt and run it like this:

$ nohup ./myScript.sh > ~/output/myOutput.txt

Start a program in the background using the nohup command

If you want the program to run in the background, you can add an ampersand. But when you do that, the program disappears. To get the program back to the terminal, use the fg command.

The output log of this command will be saved in the nohup.out file, which you can view using cat or other commands. The number 8699 in the second line represents the process number of the command, which is the pid. We can use the ps command to find this process.

Use nohup to run multiple programs simultaneously

If you need to run multiple programs at the same time, it is not necessary to run one by one, just use the && symbol. For example, if you want to run mkdir, ping, ls at the same time, you can run it like this:

$ nohup bash -c 'mkdir files &&ping -c 1 baidu.com && ls'> output.txt

Terminates the process running in the background

As mentioned above, the nohup command combined with the & symbol allows processes to run in the background, even if the terminal is shut down. Now, if you want to stop this process, what do you do?

The simplest is the kill command, which I believe you have used many times.

$ kill -9 PID

So how do you find the pid of the process? We can use the ps command.

$ ps aux | grep myScript.sh

Or you can use the pgrep command.

Next, use the kill command to terminate the process.

$ kill -9 14942

Sum up

This article about nohup running php scripts in the linux background is introduced to this, more related php programs in the linux background execution content please search the script home previous articles or continue to browse the following related articles hope that you will support the script home in the future!

Related article

Latest comments