每5秒运行一次php脚本
我知道对于每次(秒或分钟)运行php脚本,我们可以使用Cron(作业或选项卡),但cron有60秒的粒度,所以我们被迫有一个无限循环运行php脚本。例如,我们可以编写下面的代码来调用它在脚本的顶部:
I know that for running php script every time (seconds or minute) we can use Cron (job or tab) but cron has a 60 sec granularity so we are forced to have an infinite loop for running php script . For example we can write the code below to call it at the top of the script:
#!/bin/bash
while [ true ]; do
#put php script here
done
但这是不合逻辑的,因为我们必须更改PHP的执行时间php.ini所以我们有很多问题(安全,溢出,...)在服务器。那么,我们应该怎么做呢?
我的问题是如何运行php脚本每5秒,在php执行时间没有问题。
but it's illogical because we must change php execution time in php.ini so we have a lot of problems (Security , overflow , ... ) in server . well, what should we do exactly ? My question is how to run php script every 5 seconds that hasn't got problems in php execution time .
使用Cron作业脚本。获得30秒的间隔,您可以延迟5秒:
Use Cron job script. Get a 30 seconds interval , you could delay by 5 seconds:
-*/5-22 * * * sleep 5;your_script.php
上述脚本将在凌晨5点至10点运行
The above script will run 5am to 10 pm
另一种选择是
You need to write a shell script like this that sleeps on the specified interval and schedule that to run every minute in cron:
#!/bin/sh
# Script: delay_cmd
sleep $1
shift
$*
然后使用您的参数计划在cron中运行:delay_cmd 5 mycommand参数
Then schedule that to run in cron with your parameters: delay_cmd 5 mycommand parameters