且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何防止PHP脚本多次运行?

更新时间:2023-02-03 21:23:15

现在我通过 ps 检查进程是否正在运行,并通过 bash 脚本扭曲 php 脚本:

Now I check whether the process is running by ps and warp the php script by a bash script:

 #!/bin/bash

 PIDS=`ps aux | grep onlytask.php | grep -v grep`
 if [ -z "$PIDS" ]; then
     echo "Starting onlytask.php ..."
     php /usr/local/src/onlytask.php >> /var/log/onlytask.log &
 else
     echo "onlytask.php already running."
 fi

并每分钟通过 cron 运行 bash 脚本.

and run the bash script by cron every minute.