Shell script is used as a daemon instance _ PHP Tutorial to ensure that PHP scripts are not suspended

Source: Internet
Author: User
Shell scripts are used as a daemon instance to ensure that PHP scripts are not suspended. I started to run a data list a few days ago. the list requires a user name, a mobile phone number, or an email address. I can easily obtain the user name list. However, A user name list starts to run a data list as many as a few days ago. the list needs to provide the user name, whether there is a mobile phone number, and whether there is a mailbox. I can easily retrieve the user name list, but 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:

Create table 'users' ('account' varchar (50) COMMENT 'username', 'has _ phone' tinyint (3) unsigned not null default '0' comment' whether there is a mobile phone number ', 'has _ email' tinyint (3) unsigned not null default '0' COMMENT 'whether there is a mailbox ', '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:

 Nums! = 500) {$ flag = false;} if ($ this-> total_nums = 10000) {exit (0); // exit after users have run, started by the Daemon} $ this-> getUsers (); $ this-> checkUserInfo (); $ this-> updateUserInfo (); sleep (1 ); // after running 500, the user takes one second to rest and protects 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


Conclusion: The above user name data is just a chestnut. Don't be too serious. million data. I guess it will take some time, because the detection interface is slow, after receiving the request, the interface connects to the table, queries the table, and returns the result. 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 ~~~

...

Related Article

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.