Home >Backend Development >PHP Tutorial >Detailed explanation of running background php service on ubuntu
Create a php service script, which is responsible for grabbing mqtt and other communication records and saving them to the database. Normally, we only need to open a terminal on the server side to run the code and capture data. However, after closing the terminal, we cannot capture data. A daemon is a process that is always running in the background. It can run independently in the background without the terminal.
1. On ubuntu, the usual practice is to run the following code as a service script.
php run.php
This way you can run it by occupying a terminal alone. But I can't use the terminal to do other things.
2. We add & at the end of the command, as follows:
php run.php &
As long as we add the symbol & at the end of the command, the started process will become a "background task". If you want to change the running "foreground task" to a "background task", you can first press ctrl + z, and then execute the bg command (to continue the execution of the most recently paused "background task"). You can also enter fg
and press Enter to switch to normal mode.
"Background tasks" have two characteristics:
Inherits the standard output (stdout) and standard error (stderr) of the current session (conversation). Therefore, all output of background tasks will still be displayed on the command line synchronously.
The standard input (stdin) of the current session is no longer inherited. You can no longer enter commands for this mission. If it attempts to read standard input, execution will halt.
As you can see, there is only one essential difference between "background tasks" and "foreground tasks": whether to inherit standard input. Therefore, while performing background tasks, the user can also enter other commands.
tail -fn 50 nohup.out
Related recommendations:
Ubuntu upgrades PHP5 to PHP7.0
Install php5.6.32 under ubuntu to complete the method of setting up the php environment
About the tutorial on setting up the php development environment for ubuntu
The above is the detailed content of Detailed explanation of running background php service on ubuntu. For more information, please follow other related articles on the PHP Chinese website!