Shell scripts share _php techniques as a daemon instance that guarantees that PHP scripts do not hang

Source: Internet
Author: User
Tags bit set php script

A few days ago started running a list of data, list needs to provide user name, whether there is a mobile phone number, whether there is a mailbox, the user list I easily get, but, the user list of 2000w, and to detect whether the user has a mobile phone number, whether there is a mailbox must be open to a security interface to a user to request, Then analyze the return value to know.

Here is the solution I am dealing with:
1. Save the 2000w list to the temporary datasheet
2. Use PHP program to get 500 users from this table every time, after detection, generate SQL update original record
3, in order to prevent the PHP program suddenly broken, with a shell script every 1 minutes detection, PHP hung up to restart
The reason I use the shell script as a daemon is that the detection interface between the phone and the mailbox is slow and it is impossible to detect 2000w users in 1-2 days.

Program Details:
1, temporarily save the user List table users, table structure as follows:

Copy Code code as follows:

CREATE TABLE ' users ' (
' Account ' varchar COMMENT ' username ',
' Has_phone ' tinyint (3) unsigned not NULL default ' 0 ' COMMENT ' has cell phone number ',
' Has_email ' tinyint (3) unsigned not NULL default ' 0 ' COMMENT ' is there a mailbox ',
' Flag ' tinyint (3) unsigned not NULL default ' 0 ' COMMENT ' sign bit ',
PRIMARY KEY (' account '),
KEY ' flag ' (' flag ')
) Engine=innodb DEFAULT charset=utf8 comment= ' list ';

I will first import more than 2000 W username to this temporary table, Has_phone and Has_email these two fields by default are 0 (NO), the flag bit flag indicate whether the user has been detected.
The following is a subset of table data:
9873aaa,0,0,0
adddwwwd876222,0,0,0
testalexlee,0,0,0
codejia.net,0,0,0
haohdouywaa21,0,0,0

2, PHP script check_users.php
After you import a list of users into a table, write a simple php script, the idea is this: each cycle from the table to take flag=0 500 users, and then ask the interface to determine whether the user has a mobile phone number, mailbox, generate a SQL, save to a sqls array, and so on 500 users all tested, Loop sqls Array, update the list of these 500 lists, and the flag flag bit set to 1, said has been detected, the next time do not get.
Because of the long PHP script code, here's a simple code description:
Copy Code code as follows:

<?php
Class users{
Private $data;
Private $sqls;
Private $nums; Determine if there are 500 users
Private $total _nums; Number of users currently detected

Fetch 500 users at a time
Private Function Getusers () {...}

Detect these 500 users and generate SQL
Private Function Checkuserinfo () {...}

Update these 500 users
Private Function Updateuserinfo () {...}

Run
Public Function run () {
$flag = true;
while ($flag) {
if ($this->nums!=) {$flag = false;}
if ($this->total_nums = = 10000) {
Exit (0); After running 1w, the user exits and is started by the daemon.
}
$this->getusers ();
$this->checkuserinfo ();
$this->updateuserinfo ();
Sleep (1); 500 users rest 1 seconds to protect the user detection interface
}
}
}

$user = new Users ();
$user->run ();
?>

This is a concise version of the PHP script, probably meaning, the beginning of the version is not $total_nums this variable, because just started running this script, found that only ran over the 4w multiple scripts to hang the ball, and later a look, because the connection database is not connected, the script has been hanging there. Adding this variable does not solve the problem, except that after each 1w user, the PHP script exits and is restarted by the following shell script.

3, Shell script as the daemon process
I added this shell script to the crontab and executed it every 1 minutes, and this shell script is very simple to detect check_ users.php If there is a process ID, if it exists, the PHP script is still running, the shell script does nothing; if it does not exist, then the PHP script has exited (0) ran out of 1w users quit, then the shell script to start the script, Go to the next 1w user list for detection.
I have mentioned above, if the PHP script is connected to the database, can not connect when the time, PHP will hang the ball there, can not quit. I put a time check in the shell script, when the PHP script process exists, the calculation has been in place for a long time, if more than I expected time, then kill the PHP script, and then restart.

The beginning of the example data, the results are similar to the following:
testalexlee,1,0,1
codejia.net,0,0,1
haohdouywaa21,1,1,1
9873aaa,0,1,1
adddwwwd876222,1,0,1

said at the end: The above user list data just raise a chestnut, don't be too serious, 2000w data, I estimate to run for some time, because the detection interface is slow, the interface after receiving the request to even table, look-up table, and then return. In fact, the best way is directly from the interface request table pull a list, and then with the shell command to deal with the results soon, but in the company is so, some things are not open, you know ~ ~ ~

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.