shell脚本作为保证PHP脚本不挂掉的守护进程实例
前几天开始跑一份数据名单,名单需要提供用户名、是否有手机号、是否有邮箱,用户名单我轻易的获取到了,但是,用户名单有2000w之多,并且去检测用户是否有手机号、是否有邮箱必须得通过一个对外开放的安全接口一个一个用户去请求,然后分析返回值才能知道。
下面是我处理的方案:
1、将2000w名单保存到临时数据表
2、用PHP程序每次从该表获取500个用户,检测完后生成SQL update原纪录
3、为了防止PHP程序突然断掉,用shell脚本每隔1分钟检测,PHP挂掉了则重启
我使用shell脚本作为守护进程的原因是,手机与邮箱的检测接口速度慢,不可能在1~2天将2000w用户检测完。
方案详细:
1、临时保存用户名单表users,表结构如下:
CREATE TABLE `users` ( `account` varchar(50) COMMENT '用户名', `has_phone` tinyint(3) unsigned NOT NULL default '0' COMMENT '是否有手机号', `has_email` tinyint(3) unsigned NOT NULL default '0' COMMENT '是否有邮箱', `flag` tinyint(3) unsigned NOT NULL default '0' COMMENT '标志位', PRIMARY KEY (`account`), KEY `flag` (`flag`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='名单表';
我先将2000多w用户名导入到这个临时表,has_phone与has_email这二个字段默认都是0(没有),标志位flag说明该用户是否已经检测完。
下面是一部分表数据:
9873aaa,0,0,0
adddwwwd876222,0,0,0
testalexlee,0,0,0
codejia.net,0,0,0
haohdouywaa21,0,0,0
2、PHP脚本check_users.php
将 用户名单导入到表之后,再写一个简单的PHP脚本,思路是这样的:每次循环从表取flag=0的500个用户,然后请求接口判断用户是否有手机号、邮箱, 生成一条SQL,保存到一个SQLS数组里,等500个用户全部检测完了之后,循环SQLS数组,更新表里这500个名单,并将flag标志位设置为1, 表示已经检测完,下次就不获取了。
由于PHP脚本代码较长,这里分享下简单的代码说明:
<?php class Users{ private $data; private $sqls; private $nums; //判断是否有500用户 private $total_nums; //当前已经检测完的用户数量 //每次取500个用户 private function getUsers(){...} //检测这500个用户并生成SQL private function checkUserInfo(){...} //更新这500个用户 private function updateUserInfo(){...} //运行 public function run(){ $flag = true; while($flag){ if($this->nums != 500){ $flag = false; } if($this->total_nums == 10000){ exit(0); //跑完1w个用户就退出,由守护进程启动 } $this->getUsers(); $this->checkUserInfo(); $this->updateUserInfo(); sleep(1); //跑完500用户休息1秒,保护用户检测接口 } } } $user = new Users(); $user->run(); ?>
上面是简洁版的PHP脚本,大概意思到了,刚开始的版本是没有$total_nums这个变量,是因为刚开始跑这个脚本的时候,发现只跑完了4w多条脚本就挂球了,后来一看,是因为连接数据库没连上,脚本一直挂在那里。加上这个变量也无法解决这个问题,只是在每次跑完1w个用户之后,PHP脚本退出,再由下面的shell脚本重新启动。
3、shell脚本作为守护进程
我把这个shell脚本加到了crontab里边,每隔1分钟执行一次,这个shell脚本很简单,检测check_users.php是否存在进程id,如果存在,则说明PHP脚本还在运行,shell脚本不做任何操作;如果不存在,则说明PHP脚本已经exit(0)跑完了1w用户退出了,那么shell脚本启动该脚本,进入下一个1w用户名单的检测。
上面我有讲到,如果PHP脚本在连接数据库的时候,无法连接上的时候,PHP会一直挂球在那里,无法退出了。我在shell脚本里加了一个时间检测,当PHP脚本进程存在的时候,计算已经存在了多长时间,如果超过了我预想的时间,则将PHP脚本kill掉,再重启。
开头的举例数据,结果类似如下:
testalexlee,1,0,1
codejia.net,0,0,1
haohdouywaa21,1,1,1
9873aaa,0,1,1
adddwwwd876222,1,0,1
说在最后:以上用户名单数据只是举个栗子,不要太认真,2000w数据,我估计要跑一段时间了,因为检测接口比较慢,接口在接到请求后还要连表,查表,再返回。其实,最好的方法还是直接从接口请求的表拉一份名单出来,再用shell命令处理下很快就有结果了,可是在公司就是这样,有些东西不开放的,你懂的~~~

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Atom editor mac version download
The most popular open source editor

SublimeText3 English version
Recommended: Win version, supports code prompts!

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.