Php write back-end operating program summary

Source: Internet
Author: User
Tags php write
Php write back-end running program summary 1. scheduled execution ???????? Just crontab, linux command, how to use it, google on your own. Just to put it simply, you need to pay attention to permissions when using crontab. O & M often starts with root, causing some file web users to have no permissions. 2. backend Guard ??????? Two steps: 1. php is required to write the summary of the backend running program.

1. scheduled execution

???????? Just crontab, linux command, how to use it, google on your own. Just to put it simply, you need to pay attention to permissions when using crontab. O & M often starts with root, causing some file web users to have no permissions.

2. backend guard

??????? In two steps: 1. you need to write an endless loop in the script. because php is not like python, I usually do {....} while (true); however, a sleep is usually added to the loop body, or the machine will be killed. 2. when starting a process, you need to add a "&"?, If you need to record the output information, you need to write php xxxx. php>/tmp/phplog &, so that the program information is recorded in the file to facilitate troubleshooting in the future.

3. operation monitoring

?????? The daemon process in the background is started, but you cannot be sure that your program will not receive a warning message. if such information appears, php will terminate the current process, at this time, the background program exits directly. Therefore, in addition to the program that handles tasks normally, you also need a program that checks the program running Status. my name is xxxxDefend. php. The following is an example of this program.

? #! /Usr/local/php5/bin/php

// Start the command
$ Action = '/usr/local/php5/bin/php xxxxxx. php ';
$ LogPath = '/tmp/logs /';


Do {
? $ Result = array ();
? Exec ("ps aux | grep 'xxxxxx. php'", $ result );

? $ IsOk = 0;
? Foreach ($ result as $ v ){
?? $ Is = strpos ($ v, $ action );
?? If (false! ==$ Is ){
??? $ IsOk ++;
??}
?}

? $ Exec = $ action. ">". $ logPath. "xxxxxx_log &";
? For ($ I = 1; $ I <= (5-$ isOk); $ I ++ ){
?? Exec ($ exec );
?}
? Sleep (5 );
} While (true );

When the program is started in the background, it will run the ps command every five seconds to check whether the program is running. if there are no or less than five programs, it will be started to five. Of course, this work can also be done by using shell for O & M, but you have to come when the O & M capability is insufficient.

4. multi-process

????? To increase efficiency, the working php usually starts multiple or even runs on multiple machines at the same time. At this time, we need to consider the problem that multiple processes process the same data at the same time. At this time, I usually want to create a queue for the task (redis is generally used, and the performance is quite good. how can we do this? google ), then the worker program pops up a job record every time. for example, if you have a large file to process, I usually process the file well, put one by one in the redis list, so that the working program can pop multiple programs together and execute them in parallel without repeating them. If there is no redis, you can use mysql to create an innodb table. before the program processes it, you must add a read lock on the data to be processed, and then add a tag after the processing, you can also directly delete the data to avoid repeated multi-process issues.

5. logs

???? Background programs generally run all the time, and no one cares about it. Therefore, log exceptions are important, because once an accident occurs, you need to find the cause by using logs, unlike the front-end program, you can echo to see where the error is. Do not be afraid to remember more logs, do not be afraid to waste space, hard disk is not worth the money, but a bug may directly affect your income. I usually remember logs like this

[Machine ip address]? [Process pid] [time] [current program file name] [number of file lines] [required parameters and information] [others]

These are the problems that can be thought of in a normal program. generally, try catch is added to the outermost layer of the program, which can capture most of the exceptions and then record them (warning that catch fails, very depressing)

6. Performance Optimization

???? For background programs like this, O & M is generally provided to the machine separately. at this time, stress testing is required to check whether the machine can run several processes, this generally takes a look at the cpu, memory, network, and hard disk usage of the machine when processing tasks. it is best to reach the maximum value at the same time, so that your machine will not be wasted, if the hard disk usage is very high and the others are very low, you need to optimize the program. in this case, the data read and written is saved in the memory for a period of time and then written to the hard disk at one time. if the cpu usage is high, that is, your algorithm is too messy. optimize it. memory and network are generally not bottlenecks. php does not use much memory, and the server must be at least a gigabit Nic, these two items are generally not bottlenecks. So I usually open another memcache for these machines. haha, no waste.

7. digress

??? A question about server close_wait. Php programmers are generally not very rigorous. few people will take the initiative to close the connection after opening a link, such as connecting to a database or memcache. many programmers create links, perform operations, and then execute the program. If php does not close the connection, the other machine will wait for the shutdown operation. The other server will see a close_wait status, the number of connections that a machine can open is more than 60 thousand. after the background programs run, the other machine quickly becomes full and cannot be connected. At this time, some modifications need to be made on both sides. On the one hand, php should take the initiative to disconnect the connection, and on the other hand, the default timeout time of close_wait should be shortened? Google on your own), I generally only have five seconds for memcache, and the database duration is 2 minutes. After such processing, the server continuity is greatly increased, and the concurrency capability is also improved.

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.