Home php教程 php手册 shell脚本作为保证PHP脚本不挂掉的守护进程实例

shell脚本作为保证PHP脚本不挂掉的守护进程实例

Jun 13, 2016 am 11:29 AM
php shell No ensure Example start supply data use of Script process need

前几天开始跑一份数据名单,名单需要提供用户名、是否有手机号、是否有邮箱,用户名单我轻易的获取到了,但是,用户名单有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='名单表';
Copy after login

我先将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();
?>
Copy after login

上面是简洁版的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命令处理下很快就有结果了,可是在公司就是这样,有些东西不开放的,你懂的~~~

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

7 PHP Functions I Regret I Didn't Know Before 7 PHP Functions I Regret I Didn't Know Before Nov 13, 2024 am 09:42 AM

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

PHP Program to Count Vowels in a String PHP Program to Count Vowels in a String Feb 07, 2025 pm 12:12 PM

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? Apr 03, 2025 am 12:03 AM

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

See all articles