PHP多进程并行执行php脚本
<?php //fork.php $cmds = [ ‘/data/wwwroot/default/test1.php‘, ‘/data/wwwroot/default/test2.php‘, ‘/data/wwwroot/default/test3.php‘ ]; foreach($cmds as $cmd) { $pid = pcntl_fork(); if ($pid == -1) { //进程创建失败 echo ‘创建子进程失败时返回-1‘; exit(-1); } else if($pid) { //父进程会得到子进程号,所以这里是父进程执行的逻辑 pcntl_wait($status,WNOHANG); } else{ //子进程处理逻辑 echo getmypid() , " \r\n"; sleep(20);// 保持20秒,确保能被ps查到 pcntl_exec(‘/usr/local/php/bin/php‘,$cmd); exit(0); } }
# php fork.php //使用php cls模式执行运行fork.php
# ps aux | grep fork.php root 61097 0.0 0.7 367880 7776 pts/1 S 14:22 0:00 php fork.php root 61098 0.0 0.7 367880 7776 pts/1 S 14:22 0:00 php fork.php root 61099 0.0 0.7 367880 7776 pts/1 S 14:22 0:00 php fork.php root 61311 0.0 0.0 103268 872 pts/2 R+ 14:22 0:00 grep --color fork.php
相关推荐
zyyjay 2020-11-09
xuebingnan 2020-11-05
samtrue 2020-11-22
stefan0 2020-11-22
yifangs 2020-10-13
songshijiazuaa 2020-09-24
hebiwtc 2020-09-18
天步 2020-09-17
83911535 2020-11-13
whatsyourname 2020-11-13
zhouyuqi 2020-11-10
Noneyes 2020-11-10
mathchao 2020-10-28
王志龙 2020-10-28
wwwsurfphpseocom 2020-10-28
diskingchuan 2020-10-23
savorTheFlavor 2020-10-23