4lqedz 发表于 2024-10-4 14:16:47

PHP 编写保护进程


    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">1.什么是<span style="color: black;">保护</span>进程</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">保护</span>进程是脱离于终端并且在后台运行的进程。<span style="color: black;">保护</span>进程脱离于终端是为了避免进程在执行过程中的信息在任何终端上<span style="color: black;">表示</span>并且进程<span style="color: black;">亦</span>不会被任何终端所产生的终端信息所打断。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">例如 apache, nginx, redis, memcache, mysql 都是<span style="color: black;">保护</span>进程, 简而言之, <span style="color: black;">保护</span>进程(daemon)<span style="color: black;">便是</span><span style="color: black;">始终</span>在后台运行的进程(daemon)。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">2.<span style="color: black;">为何</span><span style="color: black;">研发</span><span style="color: black;">保护</span>进程</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">非常多</span>程序以服务形式存在, 他<span style="color: black;">无</span>终端或UI交互, 它可能采用其他方式与其他程序交互, 如TCP/UDP Socket, UNIX Socket, fifo。程序一旦<span style="color: black;">起步</span>便进入后台, 直到满足<span style="color: black;">要求</span>他便<span style="color: black;">起始</span>处理任务。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">3.何时采用<span style="color: black;">保护</span>进程<span style="color: black;">研发</span>应用程序</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">以我当前的<span style="color: black;">需要</span>为例, 我需要运行一个程序, <span style="color: black;">而后</span>监听某端口, <span style="color: black;">连续</span>接收服务端发起的数据, <span style="color: black;">而后</span>对数据分析处理, 再将结果写入到数据库中; 我采用ZeroMQ实现数据收发。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">倘若</span>我不采用<span style="color: black;">保护</span>进程方式<span style="color: black;">研发</span>该程序,程序一旦运行就会占用当前终端窗框,还有受到当前终端键盘输入影响,有可能程序误退出。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">4.<span style="color: black;">保护</span>进程的安全问题</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">咱们</span><span style="color: black;">期盼</span>程序在非超级用户运行,<span style="color: black;">这般</span>一旦<span style="color: black;">因为</span>程序<span style="color: black;">显现</span>漏洞被黑客<span style="color: black;">掌控</span>,攻击者只能继承运行权限,而<span style="color: black;">没法</span><span style="color: black;">得到</span>超级用户权限。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">咱们</span><span style="color: black;">期盼</span>程序只能运行一个实例,不运行<span style="color: black;">同期</span>开启两个以上的程序,<span style="color: black;">由于</span>会<span style="color: black;">显现</span>端口冲突等等问题。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">运用</span>场景</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">保护</span>进程<span style="color: black;">通常</span>用于监控其他程序运行<span style="color: black;">状况</span>和执行<span style="color: black;">按时</span>任务。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">创建<span style="color: black;">保护</span>进程</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">起始</span> -&gt; fork()创建子进程 exit()使父进程退出 -&gt; setsid() 创建新会话 -&gt; chdir("/") 设置工作目录 -&gt; umask(0) 重设文件权限掩码 close() 关闭文件描述符 -&gt; 结束</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">下面以PHP的实现方式为例<span style="color: black;">来讲</span>明。在说明之前先介绍几个PHP函数。如下:</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">pcntl_fork(): 在当前进程内创建一个子进程。成功时, 在父进程执行线程内返回产生的子进程的PID, 在子进程执行线程内返回0。失败时, 在父进程上下文返回-1, 不会创建子进程, 并且会<span style="color: black;">诱发</span>一个PHP错误。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">int pcntl_fork ( void )</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">实例:</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&lt;?php</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">$pid = pcntl_fork ();</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">//父进程和子进程都会执行下面代码</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">if($pid == - 1){</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">//错误处理:创建子进程失败时返回-1.</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">die( could not fork );</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">}elseif( $pid ) {</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">//父进程会得到子进程号,<span style="color: black;">因此</span><span style="color: black;">这儿</span>是父进程执行的<span style="color: black;">规律</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">pcntl_wait ( $status ); //等待子进程中断,防止子进程<span style="color: black;">作为</span>僵尸进程。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">}else{</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">//子进程得到的$pid为0, <span style="color: black;">因此</span><span style="color: black;">这儿</span>是子进程执行的<span style="color: black;">规律</span>。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">}</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">?&gt;</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">posix_setuid(): 设置当前进程的操<span style="color: black;">功效</span>户</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">bool posix_setuid ( int $uid )</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">posix_setgid(): 设置当前进程的操<span style="color: black;">功效</span>户所属分组</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">bool posix_setpgid ( int $pid , int $pgid )</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">getmypid(): 获取当前 PHP 进程 ID。 获取当前 PHP 进程 ID。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">int getmypid ( void )</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">posix_kill(): 向指定进程发送进程信号</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">bool posix_kill ( int $pid , int $sig )</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">pcntl_signal(): 安装一个信号处理器</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">bool pcntl_signal ( int $signo , callback $handler [, bool $restart_syscalls = true ] )</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">signo 信号编号。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">handler 信号处理器<span style="color: black;">能够</span>是用户创建的函数或<span style="color: black;">办法</span>的名字, <span style="color: black;">亦</span><span style="color: black;">能够</span>是系统常量 SIG_IGN (译注:忽略信号处理程序)或 SIG_DFL(默认信号处理程序) .</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">Note: <span style="color: black;">重视</span>当你<span style="color: black;">运用</span>一个对象<span style="color: black;">办法</span>的时候, 该对象的引用计数回<span style="color: black;">增多</span>使得它在你改变为其他处理或脚本结束之前是持久存在的。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">restart_syscalls 指定当信号到达时系统调用重启<span style="color: black;">是不是</span>可用。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">system(): 执行<span style="color: black;">外边</span>程序, 并且<span style="color: black;">表示</span>输出</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">string system ( string $command [, int &amp;$return_var ] )</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&lt;?php</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">echo &lt;pre&gt;;</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">$last_line = system ( ls , $retval );</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">echo &lt;/pre&gt;;</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">?&gt;</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">PHP后台<span style="color: black;">保护</span>进程的实现方式(Linux环境)</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">应用场景</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">某些<span style="color: black;">状况</span>下, <span style="color: black;">咱们</span>需要<span style="color: black;">连续</span>的周期性的<span style="color: black;">供给</span><span style="color: black;">有些</span>服务, <span style="color: black;">例如</span>监控内存或cpu的运行<span style="color: black;">情况</span>, 这些应用与客户端是<span style="color: black;">无</span>关系的, 不是说客户端(如web界面, 手机app等)关闭了, <span style="color: black;">咱们</span>就不监控内存或cpu了,</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">为了应对这种业务场景, 后台<span style="color: black;">保护</span>进程就<span style="color: black;">能够</span>派上用场了。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">所需环境</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">Linux</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">实现方式</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">1. 准备php脚本</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">在/usr/local/src/目录下, 新建一个daemon.php脚本文件, 内如如下:</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&lt;?php</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">Class Daemon{</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">/**</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">* 初始化一个<span style="color: black;">保护</span>进程</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">* @throws Exception</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">*/</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">public function init(){</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">//创建一个子进程</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">$pid = pcntl_fork();</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">if ($pid == -1) {</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">throw new Exception(fork子进程失败);</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">} elseif ($pid &gt; 0) {</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">//父进程退出,子进程变成孤儿进程被1号进程收养,进程脱离终端</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">exit(0);</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">}</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">//创建一个新的会话,脱离终端<span style="color: black;">掌控</span>,更改子进程为组长进程</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">$sid = posix_setsid();</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">if ($sid == -1) {</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">throw new Exception(setsid fail);</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">}</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">//修改当前进程的工作目录,<span style="color: black;">因为</span>子进程会继承父进程的工作目录,修改工作目录以释放对父进程工作目录的占用。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">chdir(/);</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">/**</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">* <span style="color: black;">经过</span>上一步,<span style="color: black;">咱们</span>创建了一个新的会话组长,进程组长,且脱离了终端,<span style="color: black;">然则</span>会话组长<span style="color: black;">能够</span>申请重新打开一个终端,为了避免</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">* 这种<span style="color: black;">状况</span>,<span style="color: black;">咱们</span>再次创建一个子进程,并退出当前进程,<span style="color: black;">这般</span>运行的进程就<span style="color: black;">再也不</span>是会话组长。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">*/</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">$pid = pcntl_fork();</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">if ($pid == -1) {</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">throw new Exception(fork子进程失败);</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">} elseif ($pid &gt; 0) {</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">//再一次退出父进程,子进程<span style="color: black;">作为</span><span style="color: black;">最后</span>的<span style="color: black;">保护</span>进程</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">exit(0);</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">}</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">//<span style="color: black;">因为</span><span style="color: black;">保护</span>进程用不到标准输入输出,关闭标准输入,输出,错误输出描述符</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">fclose(STDIN);</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">fclose(STDOUT);</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">fclose(STDERR);</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">}</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">}</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">$daemon = new Daemon();</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">$daemon-&gt;init();</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">//处理业务代码</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">while(true) {</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">file_put_contents(/usr/local/src/log.txt, time().PHP_EOL, FILE_APPEND);</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">sleep(5);</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">}</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">该脚本的<span style="color: black;">功效</span>, <span style="color: black;">便是</span>每隔5秒, 向日志文件中写入一个时间戳, 当然, 这个只是一个简单的示例, <span style="color: black;">详细</span>应用中, <span style="color: black;">咱们</span>还需要<span style="color: black;">按照</span>业务的<span style="color: black;">区别</span>, 编写<span style="color: black;">详细</span>的业务处理代码。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">2. 以后台方式运行php脚本</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">在命令行下, 输入:</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">nohup php /usr/local/src/daemon.php &amp;</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">nohup: ignoring input and appending output to `nohup.out</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">nohup: failed to run command `/etc/nginx_check.sh: Permission denied</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">说明<span style="color: black;">无</span>权限</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">chmod +x /usr/local/src/daemon.php</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">nohup: ignoring input and appending output to ‘nohup.out’</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">倘若</span>只<span style="color: black;">显现</span>这种结果: 说明<span style="color: black;">保护</span>进程执行成功, <span style="color: black;">运用</span>ctrl+c 退出</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">3. 查看日志输出</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">tail -f /usr/local/src/log.txt</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">咱们</span>将会看到如下信息:</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">1471917997</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">1471918016</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">1471918026</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">4. 关闭php后台进程</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">首要</span>, <span style="color: black;">咱们</span>需要查出该进程的PID, 命令:</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">ps -ef | grep "php /usr/local/src/daemon.php" (常用的参数 ps -A | grep "daemon.php")</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">经过</span>这个PID把该进程kill掉</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">kill -9 22767</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">其中, 22767<span style="color: black;">便是</span>php后台进程的PID号。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">5. 开机自启</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">经过</span>前面的<span style="color: black;">过程</span>, <span style="color: black;">咱们</span><span style="color: black;">晓得</span><span style="color: black;">怎样</span>开启和关闭一个php进程, <span style="color: black;">然则</span>, 在<span style="color: black;">实质</span>的应用中, <span style="color: black;">咱们</span>不可能每次都是手动开启, <span style="color: black;">这般</span><span style="color: black;">咱们</span>就会损失掉一部分业务数据, <span style="color: black;">因此</span><span style="color: black;">咱们</span>必须要让该进程开机自动运行, <span style="color: black;">过程</span>如下:</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">在/etc/rc.local文件中, 将nohup php /usr/local/src/daemon.php &amp;这个命令加入<span style="color: black;">就可</span>。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">重视</span>: <span style="color: black;">保护</span>进程文件加载<span style="color: black;">尽可能</span>采用绝对<span style="color: black;">位置</span>, <span style="color: black;">能够</span><span style="color: black;">运用</span> __dir__ 代替;</p>
    <div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/tos-cn-i-qvj2lq49k0/403bc2febc914dc39e823211f8eca62b~noop.image?_iz=58558&amp;from=article.pc_detail&amp;lk3s=953192f4&amp;x-expires=1728211693&amp;x-signature=77aOqchknJVZhmLGIWyuNVxr3hw%3D" style="width: 50%; margin-bottom: 20px;"></div>




nykek5i 发表于 2024-10-7 19:42:21

你的话语如春风拂面,让我心生暖意。

nykek5i 发表于 2024-10-15 10:19:42

楼主听话,多发外链好处多,快到碗里来!外链论坛 http://www.fok120.com/

nqkk58 发表于 6 天前

我完全同意你的观点,说得太对了。
页: [1]
查看完整版本: PHP 编写保护进程