Instead of bash, write PHP daemon (background program) _php Tutorial

Source: Internet
Author: User
Help the Home (www.Bkjia.com) tutorial recently in a Yahoo friend and I said, before using Bash CSH script, it is now in PHP to write a daemon, think PHP function is the most complete, and easy to use shell_exec easily call the system kernel.

I had a conversation with him. Some principles of background program are summarized as follows.

The following is the referenced content:
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, to remove the previous script to write some of the thinking habits. Once the page script is run, the memory is released immediately. and the daemon program is different, it will run for a year, even years.

1. As a good habit, be sure to run the code inside a dead loop such as while (1) {}. This way, the script does not stop as long as the code is not in a state.

2. Echo is not used, but rather log instead. Use the Write log method instead of ECHO. Because Echo is outputting a character to the screen, if there are no output objects, a fatal error is reported.

3. If MySQL, every time you reconnect MySQL.

Above is an example of a connection. This code was once criticized by a master mercilessly. This is mainly to prevent the following things:

MySQL restarts, $db variable is definitely a resource, but this resource is invalid. If you execute the code again, it will happen:

MySQL has go away such errors. This will output to the screen, and the instant Shutdown error report will also output. This leads to the entire

The script runs incorrectly, just like the error echo produces.

Although each connection to the database is a bit wasteful, but also can only err on the wrong kill 1000, also do not miss one. This is a mistake that most friends make.

Error, many people and I said this is a bug, because, originally wrote a good file, and then connect with MySQL error.

It's not actually a MySQL bug.

4. The newly generated variable, if not automatically released, should be released immediately. Otherwise, the program will collapse. Many PHP programmers do not have a bit

Memory management concept, feel that the memory is unlimited, casual use, write the background program must pay attention to the management of memory.

5. If you want to access the file, first of all to Clearstatcache, otherwise it is likely inaccurate statistics, or

It is inaccurate to determine if the file exists. What's more, if you open the file frequently, the handle value of the file will

The program will not be able to open the file until the maximum value of the integer has been added. A lot of people program that 3 months hang once,

There is no error, and there is no memory problem, it is likely that the statcache is not cleared before each operation of the file.

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

http://www.bkjia.com/PHPjc/364310.html www.bkjia.com true http://www.bkjia.com/PHPjc/364310.html techarticle Fire Net (liehuo.net) tutorial recently a friend of Yahoo and I said, before using Bash CSH script, it is now in PHP to write the background program, think PHP functions most complete, and very easy to use ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.