I'm having trouble running a single instance of a PHP script using CRON. Perhaps someone can help explain what is needed. Currenty, I have a startup
script that is called by crontab
which checks to make sure an instance of a PHP script isn't already running before calling the PHP instance.
我在使用CRON运行PHP脚本的单个实例时遇到问题。也许有人可以帮助解释所需要的东西。 Currenty,我有一个由crontab调用的启动脚本,它在调用PHP实例之前检查以确保PHP脚本的实例尚未运行。
crontab -e
entry:
crontab -e条目:
* * * * * /var/www/private/script.php >> /var/www/private/script.log 2>&1 &
./startup
#!/bin/bash
if ps -ef | grep '[s]cript';
then
exit;
else
/usr/bin/php /var/www/private/script.php >>/var/www/private/script.log 2>&1 &
echo 'started'
fi
This doesn't seem to be working and I can't seem to get any errors logged to know how to proceed to debug this.
这似乎没有用,我似乎无法记录任何错误,知道如何继续调试。
2 个解决方案
#1
1
You can use lockrun for that: http://www.unixwiz.net/tools/lockrun.html
您可以使用lockrun:http://www.unixwiz.net/tools/lockrun.html
* * * * * /usr/local/bin/lockrun --lockfile=/var/run/script.lockrun -- php /home/script.php
Or use Perl:
或者使用Perl:
system('php /home/script.php') if ( ! `ps -aef|grep -v grep|grep script.php`);
#2
0
Testing processes is not the most reliable way to prevent multiple instance of a script.
测试过程不是防止脚本多个实例的最可靠方法。
In bash
, I recommend you to use that :
在bash中,我建议你使用它:
if ( set -o noclobber; echo "locked" > "$lockfile") 2> /dev/null; then
trap 'rm -f "$lockfile"; exit $?' INT TERM EXIT
echo "Locking succeeded" >&2
rm -f "$lockfile"
else
echo "Lock failed - exit" >&2
exit 1
fi
There's another examples in this page http://wiki.bash-hackers.org/howto/mutex
此页面中还有另一个示例http://wiki.bash-hackers.org/howto/mutex
Another solution (if not using NFS
) is to use flock (1)
, see How do I use the linux flock command to prevent another root process from deleting a file?
另一个解决方案(如果不使用NFS)是使用flock(1),请参阅如何使用linux flock命令阻止其他根进程删除文件?
#1
1
You can use lockrun for that: http://www.unixwiz.net/tools/lockrun.html
您可以使用lockrun:http://www.unixwiz.net/tools/lockrun.html
* * * * * /usr/local/bin/lockrun --lockfile=/var/run/script.lockrun -- php /home/script.php
Or use Perl:
或者使用Perl:
system('php /home/script.php') if ( ! `ps -aef|grep -v grep|grep script.php`);
#2
0
Testing processes is not the most reliable way to prevent multiple instance of a script.
测试过程不是防止脚本多个实例的最可靠方法。
In bash
, I recommend you to use that :
在bash中,我建议你使用它:
if ( set -o noclobber; echo "locked" > "$lockfile") 2> /dev/null; then
trap 'rm -f "$lockfile"; exit $?' INT TERM EXIT
echo "Locking succeeded" >&2
rm -f "$lockfile"
else
echo "Lock failed - exit" >&2
exit 1
fi
There's another examples in this page http://wiki.bash-hackers.org/howto/mutex
此页面中还有另一个示例http://wiki.bash-hackers.org/howto/mutex
Another solution (if not using NFS
) is to use flock (1)
, see How do I use the linux flock command to prevent another root process from deleting a file?
另一个解决方案(如果不使用NFS)是使用flock(1),请参阅如何使用linux flock命令阻止其他根进程删除文件?