Home > Backend Development > PHP Tutorial > Instead of bash, write PHP daemon (background program)_PHP tutorial

Instead of bash, write PHP daemon (background program)_PHP tutorial

WBOY
Release: 2016-07-21 14:56:10
Original
744 people have browsed it

Bkjia (www.Bkjia.com) Tutorial Recently, a friend at Yahoo told me that I used to use bash csh to write scripts, but now I use PHP to write background programs. I feel that PHP’s functions are the most Complete and easy to use shell_exec to easily call the system kernel.

I exchanged some principles with him about writing background programs, which are summarized as follows.

以下为引用的内容:
function connnect()
{
global $db;
if (is_resource($db)) {
mysqli_close($db);
}
$db = mysqli_connect("122.225.96.142", 'waihui', 'freebsd@fzm', 'waihui');
}

First of all, we need to get rid of some of the previous thinking habits of writing web scripts. After the web script is run once, the memory is released immediately. The daemon program is different. It will run for a year or even several years.

1. As a good habit, you must run the code in an infinite loop like while (1) {}. In this way, the script will not stop as long as the code does not fail.

 2. Echo cannot be used, but use log instead. Use logging instead of echo. Because echo outputs a character to the screen, if there is no output object, a fatal error will be reported.

 3. If it is MYSQL, you need to reconnect to MYSQL every time.

The above is an example of connection. This code was once severely criticized by an expert. This is mainly to prevent the following things:

Mysql has restarted. The $db variable is definitely still a resource, but this resource is no longer valid. It will happen if you execute the code again:

 mysql has go away such an error. This will be output to the screen, even if error reporting is turned off. This leads to the entire

The script execution error is the same as the error generated by echo.

Although it is a waste to connect to the database every time, we can only kill 1000 by mistake rather than let one go. This mistake is made by most friends

Error, many people told me that this is a mysql bug, because originally writing the file was fine, but then an error occurred when connecting to mysql.

Actually it is not a mysql bug.

4. Newly generated variables, if they are not automatically released, must be released immediately. Otherwise, over time, the program will crash. Many PHP programmers don’t have this

Regarding the concept of memory management, I think that memory is unlimited and can be used casually. When writing background programs, you must pay attention to memory management.

5. If you want to access files, you must first clearstatcache, otherwise there is a high risk of inaccurate statistics, or

It is no longer accurate to determine whether a file exists. What's even worse is that if you open files frequently, the handle value of the file will be

Keep increasing until it exceeds the maximum value of the integer, and the program cannot open the file. Many people’s programs fail once every 3 months,

There is no error or memory problem. It is very likely that the statcache is not cleared before each file operation.

Reprinted from: http://www.cnblogs.com/niniwzw/

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/364310.htmlTechArticleLieHuo.Net Tutorial Recently, a friend at Yahoo told me that I used to use bash csh to write scripts. , it is now using PHP to write backend programs. I feel that PHP has the most comprehensive functions and is easy to use...
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template