1. Timed execution
On the crontab, Linux commands, how to use, their own Google. Just to say, crontab need to pay attention to the permissions, operations are often started with root, resulting in some files Web users do not have permissions.
2. Backstage Guard
In two steps: 1 need to write a dead loop in the script, because PHP is not like python, I am generally do {...} while (true); But the circulation body inside generally want to add a sleep, otherwise the machine will be run to die; 2 start the process need to add a "&", this everyone also Google Bar, if you need to record the output of the information needed to write PHP xxxx.php >/tmp/phplog &, so that the information of the program is recorded in the file, convenient to troubleshoot problems later.
3. Operation Monitoring
Daemon Daemon started, but you can not 100% sure your program does not appear warning message, once this information appears, PHP will terminate the current process, this time the background of the program directly exited. So in addition to the normal process of things, but also need a check the program running state of the program, I usually call xxxxdefend.php, this program shows such as the next
#!/usr/local/php5/bin/php
<?php
Commands to start
$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 <= ($isOk); $i + +) {
EXEC ($exec);
}
Sleep (5);
} while (true);
The program is also in the background to start up, it will be every 5 seconds with the PS command to check the existence of the program, not or less than 5 when the start to 5. Of course, this work can also be handed to Ops to do with the shell, but when the operational capacity is not enough, you have to do it yourself.
4. Multi-process
PHP works in order to increase efficiency, in general, it will start multiple at the same time, or even run on more than one machine. This is the time to take into account the multi-process processing of the same data at the same time. This time generally I will do a queue of tasks (generally with redis, this performance pretty good, how to do, everyone or Google bar), and then work program each pop out a record to work, such as you have a large file to deal with, this time I generally handle the file well, A single piece of the Redis list, so that the program can be more than one program pop, parallel execution, and will not be repeated. If there is no redis, you can do with MySQL, build a innodb table, the program must be processed before the data to be processed to add a read lock, and then after processing the tag, or directly delete the data, which can also achieve multi-process non-repetition problem.
5. Log
Backstage program generally will always run, basically no accident no one listens to it, so the log is very important, because once the accident, need to rely on the log to find the reason, unlike the foreground of the program can echo see where the wrong. Log don't be afraid to remember more, do not be afraid of wasting space, hard disk is not valuable, but a bug may directly affect your income. I usually keep a diary like this.
[Machine IP] [Process PID] Time [Current program file name] [Number of file lines] [Necessary parameters and information] Other
These are normal procedures can be thought of the problem, usually also in the outermost layer of the program to add a try catch, so that can catch most of the exceptions, and then also recorded (warning catch not, quite depressed)
6. Performance optimization
Like this backstage program, general operations will be given to the machine alone, this time need to do a stress test, to see how the machine can run several processes, this is generally in the process of processing the task of the process to see the CPU, memory, network, and the use of the hard disk, preferably these simultaneously reached the maximum value, So that your machine will not waste, if the hard disk usage is very high, the other is very low, it is necessary to optimize the program, this is usually read and write data in memory for a period of time, and then write to the hard disk; If the CPU is high, it is your algorithm is too rubbing, optimization optimization bar , memory and network generally do not become a bottleneck, PHP does not have much memory, the server must be at least a gigabit network card, these two are generally not a bottleneck. So these machines I will usually open a memcache, haha, not wasted.
7. Off-Topic
A question about server close_wait. PHP programmers are generally less rigorous, few people open the link after the active shutdown connection, such as database, even memcache, many programmers are built links, operations, and then the program to complete the execution. If PHP does not actively close the connection, will cause the other side of the machine will always wait for this side of the shutdown operation, on the other side of the server to see is a close_wait state, and a machine can open the link is so more than 60,000, especially after the background of the program run up, the other side of the machine is quickly occupied, And not even. This time 2 sides need to make some changes, on the one hand PHP to actively disconnect, on the other hand the machine, need to close_wait the default timeout time to change a little (how to change?) Own Google to go), I generally memcache only 5 seconds, the database of the long point, also 2 minutes. After this processing, the server's durability is greatly increased and the concurrency capability increases.