Instead of bash, write PHPdaemon (background program ). LieHuo. net) a friend from Yahoo recently told me that he used to write scripts with bashcsh. now he is using PHP to write background programs. he thinks PHP has the most complete function, in addition, it is easy to use the help House (www.Bkjia.com) tutorial. Recently, a friend from Yahoo told me that he used to write scripts using bash csh. now he is using PHP to write background programs, I think PHP has the most complete functions and it is easy to use shell_exec to call the system kernel.
I spoke with him about some principles for writing background programs.
Reference content is 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, we need to remove some of the previous thinking habits of writing web scripts. The memory will be released immediately after the Web script runs once. The daemon program runs for a year or even a few years.
1. as a good habit, you must run the code in an endless loop such as while (1. In this way, the script will not stop as long as the code is not working properly.
2. echo cannot be used, but log is used instead. Use the log writing method instead of echo. Because echo outputs a character to the screen, if there is no output object, a fatal error is reported.
3. if MYSQL is to be reconnected.
The above is an example of connection. This code was once criticized by a master. This is mainly to prevent the following:
After mysql is restarted, the $ db variable must be a resource, but this resource is no longer valid. If you execute the code again, the following occurs:
Errors such as mysql has go away. This will be output to the screen, and the instant close error report will also be output. This leads to the entire
A script running error is the same as an echo error.
Although it is a waste to connect to the database each time, you can only kill 1000 by mistake and never miss one. Most of my friends make this mistake.
This is a mysql bug. many people tell me this is because the file was properly written and an error occurred when connecting to mysql.
It is not a mysql bug.
4. new variables should be released immediately if they are not automatically released. Otherwise, the program will crash over time. Many PHP programmers have no
In terms of memory management, I think the memory is infinite and can be used at will. to write background programs, pay attention to memory management.
5. if you want to access a file, clearstatcache is required first. Otherwise, the statistics may be inaccurate, or
It is inaccurate to determine whether the file is changed. Even worse, if you open files frequently, the handle value of the file will
The program will not be able to open files until the maximum integer value is exceeded. A lot of people go through the program once every three months,
No error or memory problem occurs. it is very likely that the statcache is not cleared before each file operation.