Home >Backend Development >PHP Tutorial >Shell scripts are shared as daemon examples to ensure that PHP scripts do not hang up_PHP Tutorial
I started running a data list a few days ago. The list requires user names, whether they have mobile phone numbers, and whether they have email addresses. I easily obtained the user list. However, there are as many as 20 million users in the list, and I have to check whether the user has Whether there is a mobile phone number and an email address must be requested one by one through a secure interface that is open to the outside world, and then the return value can be analyzed to know.
The following is the solution I dealt with:
1. Save the 2000w list to a temporary data table
2. Use a PHP program to retrieve the list every time Get 500 users from the table, and generate the original SQL update record after detection
3. In order to prevent the PHP program from suddenly disconnecting, use a shell script to detect every 1 minute. If PHP fails, restart it
I use the shell script as The reason for the daemon process is that the detection interface between mobile phones and mailboxes is slow, and it is impossible to detect 20 million users in 1 to 2 days.
Details of the plan:
1. Temporarily save the user list table users, the table structure is as follows:
3. Shell script as a daemon
I added this shell script to crontab and executed it every 1 minute. This shell script is very simple. It detects whether the process id exists in check_users.php , if it exists, it means that the PHP script is still running, and the shell script does not do anything; if it does not exist, it means that the PHP script has finished exit(0) and the 1w user has exited, then the shell script starts the script and enters the next 1w Detection of user lists.
I mentioned above that if the PHP script cannot connect to the database when it is connected, PHP will always hang there and cannot exit. I added a time detection in the shell script. When the PHP script process exists, calculate how long it has existed. If it exceeds the time I expected, kill the PHP script and restart it.
, 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
Finally: The above user list data is just an example, don’t take it too seriously. With 20 million data, I estimate it will take a while because the detection interface is relatively slow. After receiving the request, you need to connect the table, look up the table, 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 it, and you will get the result quickly. But this is the case in the company, some things are not open, you know~~~