PHP implements program singleton operation
1. Scene description:
Recently, in our business, we need to constantly monitor changes in a directory. If there are files in the directory, start a PHP script to process them. The initial solution is to use crontab to execute the sh script. The script is roughly as follows:
SOK=`ps -ef |grep /www/sender.sh | grep -v grep|wc -l` if [[ $SOK < 2 ]];then for f in `ls /www/queue`; do php /www/logsender.php /www/queue/$f done
An exception occurred during actual operation: the method of ps -ef | grep xxx may not be able to correctly determine whether the process is executing, and the if condition will never be established, causing the PHP script to never be executed. After consideration, we decided to create a class that is independent of other modules and can implement singleton operation of the process to solve this problem.
2. Scheme design
1. Implement process singleton through PID file
2. Automatically create and delete PID files when the program starts and exits, so that no business code is required to consider PID file deletion
3. Try to ensure code independence without affecting business code
3. Principle
1. Start creating PID file
2. Bind semaphores such as program exit and kill, used to delete PID files
3. Add a destructor and delete the PID file when the object is destroyed
4. Problems encountered
When the program exits normally, the semaphore cannot be captured. I don’t know if the semaphore is selected wrongly. Signals such as ctrl+c are normal. If the semaphore can be captured when the program exits normally, it can be replaced by destruction. Function solution, more stable
5. Code
<!--?php /** * Created by PhpStorm. * User: huyanping * Date: 14-8-13 * Time: 下午2:25 * * 实现程序单例运行,调用方式: * declare(ticks = 1);//注意:一定要在外部调用文件中首部调用该声明,否则程序会无法监听到信号量 * $single = new DaemonSingle(__FILE__); * $single--->single(); * */ class DaemonSingle { //PID文件路径 private $pid_dir; //PID文件名称 private $filename; //PID文件完整路径名称 private $pid_file; /** * 构造函数 * @param $filename * @param string $pid_dir */ public function __construct($filename, $pid_dir='/tmp/'){ if(empty($filename)) throw new JetException('filename cannot be empty...'); $this->filename = $filename; $this->pid_dir = $pid_dir; $this->pid_file = $this->pid_dir . DIRECTORY_SEPARATOR . substr(basename($this->filename), 0, -4) . '.pid'; } /** * 单例模式启动接口 * @throws JetException */ public function single(){ $this->check_pcntl(); if(file_exists($this->pid_file)) { throw new Exception('the process is already running...'); } $this->create_pid_file(); } /** * @throws JetException */ private function create_pid_file() { if (!is_dir($this->pid_dir)) { mkdir($this->pid_dir); } $fp = fopen($this->pid_file, 'w'); if(!$fp){ throw new Exception('cannot create pid file...'); } fwrite($fp, posix_getpid()); fclose($fp); $this->pid_create = true; } /** * 环境检查 * @throws Exception */ public function check_pcntl() { // Make sure PHP has support for pcntl if (!function_exists('pcntl_signal')) { $message = 'PHP does not appear to be compiled with the PCNTL extension. This is neccesary for daemonization'; throw new Exception($message); } //信号处理 pcntl_signal(SIGTERM, array(&$this, signal_handler)); pcntl_signal(SIGINT, array(&$this, signal_handler)); pcntl_signal(SIGQUIT, array(&$this, signal_handler)); // Enable PHP 5.3 garbage collection if (function_exists('gc_enable')) { gc_enable(); $this->gc_enabled = gc_enabled(); } } /** * 信号处理函数,程序异常退出时,安全删除PID文件 * @param $signal */ public function signal_handler($signal) { switch ($signal) { case SIGINT : case SIGQUIT: case SIGTERM:{ self::safe_quit(); break; } } } /** * 安全退出,删除PID文件 */ public function safe_quit() { if (file_exists($this->pid_file)) { $pid = intval(posix_getpid()); $file_pid = intval(file_get_contents($this->pid_file)); if($pid == $file_pid){ unlink($this->pid_file); } } posix_kill(0, SIGKILL); exit(0); } /** * 析构函数,删除PID文件 */ public function __destruct(){ $this->safe_quit(); } }

ace-guard client exe是腾讯代理游戏的反作弊程序,是ewido的守护进程,保护“ewido.exe”进程不被恶意软件关闭;使用它可以检测游戏用户是否有开挂行为,可自动进行封号处理。

要自动化任务和管理多个系统,任务计划软件是您武器库中的宝贵工具,尤其是对于系统管理员而言。Windows任务计划程序完美地完成了这项工作,但最近许多人报告说操作员拒绝了请求错误。该问题存在于操作系统的所有迭代中,即使已经广泛报告和涵盖,也没有有效的解决方案。继续阅读以找到真正对其他人有用的内容!操作员或管理员拒绝了任务计划程序0x800710e0中的请求是什么?任务计划程序允许在没有用户输入的情况下自动执行各种任务和应用程序。您可以使用它来安排和组织特定应用程序、配置自动通知、帮助传递消息等。它

Windows的操作随着每个版本而变得越来越好,具有诱人的功能来改善用户体验。用户希望在Windows10和11上探索的一项功能是能够按面部对照片进行排序。此功能允许您通过面部识别对朋友和家人的照片进行分组。听起来很有趣,对吧?继续阅读如何了解如何利用该功能。我可以在Windows上按面孔对照片进行分组吗?是的,您可以使用“照片”应用在Windows10和11上按人脸对图片进行分组。但是,此功能在照片应用程序版本上不可用。此外,您可以使用“人脉”选项卡将这些照片链接到联系人。因此,使用此功能可以

在iOS中,当您将iPhone从纵向旋转到横向时,许多App会显示不同的视图。根据应用程序及其使用方式,这种行为并不总是可取的,这就是Apple在“控制中心”中包含方向锁定选项的原因。但是,某些应用程序在禁用方向锁定的情况下工作得更有用-想想YouTube或照片应用程序,将设备旋转到横向可以提供更好的全屏观看体验。如果您倾向于保持锁定状态,则必须在每次打开这些类型的应用程序时在“控制中心”中禁用它以获得全屏体验。然后,当您关闭应用程序时,您必须记住重新打开方向锁定,这并不理想。幸运的是,您可以创

“microsoft visual c++”是可以卸载的,但是不建议卸载;“microsoft visua”这些都是一些微软的组件,里面包括一些“C++”标准库、原始数据库等相关信息,很多软件尤其是游戏中需要“microsoft visual c++”中的环境组件,如果缺少了“C++”标准库的支持,可能会造成软件的无法运行。

卸载程序的文件名是“uninstall.exe”或“uninst.exe”,是用以协助使用者将软件自电脑中删除的一种电脑软件。使用方法:1、在文件资源管理器中挖掘并导航到应用程序EXE文件所在的文件路径;2、通过文件路径打开应用程序的安装目录,找到“uninstall.exe”文件;3、双击卸载文件“uninstall.exe”即可开始程序删除过程。

Microsoft应用商店是内置存储库,用户可以在其中下载、更新和卸载适用于Windows操作系统的应用。可悲的是,许多用户不知道如何在MicrosoftStore上卸载应用程序。因此,本文将带您了解如何快速从Microsoft商店卸载应用程序。或者,如果您的Windows11PC上缺少Microsoft应用商店应用程序,我们提供了有关下载和安装应用商店应用程序的详细指南。是否可以直接从Microsoft应用商店卸载应用?否,Microsoft应用商店不提供直接从平台卸载应用的选项。您只能通过平

给定一个方阵M[r][c],其中“r”是一定数量的行,“c”是列,使得r=c,我们必须检查“M”是否是单位矩阵。恒等矩阵恒等矩阵也称为大小为nxn方阵的单位矩阵,其中对角元素的整数值为1,非对角元素的整数值为0就像下面给定的示例-$$I1=\begin{bmatrix}1\end{bmatrix},\I2=\begin{bmatrix}1&0\0&1\end{bmatrix},\I3=\begin{bmatrix}1&0&0\0&1&0\0&


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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Dreamweaver CS6
Visual web development tools

SublimeText3 Linux new version
SublimeText3 Linux latest version

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),
