Share a shell script as a daemon instance that prevents PHP scripts from being suspended

Source: Internet
Author: User
The following is an analysis of the Daemon instance that uses shell scripts to ensure that PHP scripts are not suspended. For more information, see the following, the user name, mobile phone number, and email address are required for the list. I can easily obtain the user name list. However, there are as many as user names, in addition, to check whether a user has a mobile phone number or an email address, the user must send requests one by one through an open security interface, and then analyze the returned values to know.

The following is my solution:
1. Save the million list to the temporary data table
2. use the PHP program to obtain 500 users from the table each time. After detection, the original SQL update record is generated.
3. to prevent the PHP program from being suddenly disconnected, use the shell script to check every one minute. if the PHP program fails, restart it.
The reason why I use shell scripts as the daemon is that the detection interface between mobile phones and mailboxes is slow, and it is impossible ~ Million users will be detected in 2 days.

Solution details:
1. temporarily save the username list users. the table structure is as follows:
The code is as follows:
Create table 'users '(
'Account' varchar (50) COMMENT 'username ',
'Has _ phone' tinyint (3) unsigned not null default '0' comment' indicates whether the mobile phone number exists ',
'Has _ email 'tinyint (3) unsigned not null default '0' comment' whether mailbox exists ',
'Flag' tinyint (3) unsigned not null default '0' comment' flag ',
Primary key ('account '),
KEY 'flag' ('flag ')
) ENGINE = InnoDB default charset = utf8 COMMENT = 'list table ';

I will first import more than 2000 million user names to this temporary table. the has_phone and has_email fields are both 0 (none) by default. flag indicates whether the user has been detected.
Below are some table data:
9873aaa, 0, 0
Adddwwwd876222, 0, 0
Testalexlee, 0, 0
Codejia.net, 0, 0
Haohdouywaa21, 0, 0

2. PHP script check_users.php
After importing a user name to a table, write a simple PHP script. The idea is as follows: each cycle retrieves the 500 users whose flag is 0 from the table, then, request the interface to determine whether the user has a mobile phone number or email address, generate an SQL statement, and save it to an SQLS array. after all the 500 users are detected, cycle the SQLS array, update the 500 list in the table and set the flag to 1, which indicates that the list has been detected and will not be obtained next time.
Due to the long PHP script code, here is a simple code description:
The code is as follows:
Class Users {
Private $ data;
Private $ sqls;
Private $ nums; // determines whether there are 500 users
Private $ total_nums; // Number of users that have been detected

// Get 500 users each time
Private function getUsers (){...}

// Detect these 500 users and generate SQL
Private function checkUserInfo (){...}

// Update the 500 users
Private function updateUserInfo (){...}

// Run
Public function run (){
$ Flag = true;
While ($ flag ){
If ($ this-> nums! = 500) {$ flag = false ;}
If ($ this-> total_nums = 10000 ){
Exit (0); // exit after users run, started by the Daemon
}
$ This-> getUsers ();
$ This-> checkUserInfo ();
$ This-> updateUserInfo ();
Sleep (1); // after running 500 users, rest for 1 second to protect the user detection interface
}
}
}

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

The above is a simple PHP script, which probably means that the variable $ total_nums does not exist in the first version, because at the beginning of this script, I found that I only ran more than scripts and hung the ball. later, I checked that the script was always hung there because the database was not connected. This variable cannot solve this problem, but after users are run each time, the PHP script exits and the following shell script restarts.

3. shell scripts as daemon
I added this shell script to crontab and executed it every one minute. this shell script is very simple. it checks whether check_users.php has a process id. if so, it indicates that the PHP script is still running, the shell script does not perform any operations. if it does not exist, it indicates that the PHP script has been exit (0) and users have exited. then the shell script starts the script and checks the next 1 W user name list.
As I mentioned above, if the PHP script cannot be connected when it is connected to the database, the PHP script will be suspended and cannot exit. I added a time check to the shell script. when the PHP script process exists, it calculates how long it has existed. if it exceeds the expected time, kill the PHP script, restart.

The example data at the beginning is as follows:
Testalexlee, 1, 0, 1
Codejia.net, 0, 0, 1
Haohdouywaa21, 1, 1
9873aaa, 0, 1, 1
Adddwwwd876222, 1, 0, 1

At the end:The preceding User name single data is just an example. Don't be too careful. million data. I guess it will take some time. because the detection interface is slow, the interface will have to connect to the table after receiving the request, and then return. In fact, the best way is to pull a list directly from the table requested by the interface, and then use shell commands to process the results quickly, but in the company, this is the case, 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.