bash脚本,执行PHP文件每5秒 [英] Bash script that executes php file every 5 seconds
问题描述
Bash脚本:
$猫test.sh
#!/斌/庆典
而真正的
做
/home/user/public_html/website.com/test.php
睡眠5
DONE
我试着用cron来调用它:
* * * * * /home/user/public_html/website.com/test.sh
哪里是我的错?不要紧,其中.SH所在?
编辑:
我固定从上面的所有问题。我也创造了这个cron作业prevent多个proccesses:
* * * * *羊群-n /tmp/test.lock /home/user/public_html/website.com/test.sh
现在的问题是,在bash执行每5秒的PHP,这将创建类似的多个proccesses:
用户12:31 /home/user/public_html/website.com/test.php
用户12:31 /home/user/public_html/website.com/test.php
用户12:31 /home/user/public_html/website.com/test.php
如何控制呢?我需要改变bash脚本,所以如果在执行PHP它可以检测。
这是正确的:
#!/斌/庆典
而真正的
如果[$(的pidof test.php的)]
然后
回声暗战
其他
做
/home/user/public_html/website.com/test.php&安培;
睡眠5
科幻
DONE
这是另一种答案是运行你只有12次的脚本,并把它放在crontab中
#!/斌/庆典
在1 2 3 4 5 6 7 8 9 0 1 2环;做
/home/user/public_html/website.com/test.php&安培;
睡眠5
DONE
- 或 -
#!/斌/庆典
循环= 0
而[$循环-lt 12]。做
/home/user/public_html/website.com/test.php&安培;
睡眠5
循环= $(($循环+ 1))
DONE
- 更新 -
如果你的目标是不是有多个test.php的,使用这个脚本:(只运行一次,不要把它在crontab):
#!/斌/庆典而真实的;做
开始=`日期+%s`
/home/user/public_html/website.com/test.php
结束=`日期+%s`
如果[$(($结束 - $开头))-lt 5];然后
睡眠$(($开头+ 5 - $结束))
科幻
DONE
说明:这个脚本调用test.php的,并等待它终止(因为它剂量没有&安培;
到底)。然后,它测量时间:如果它已经过去了5秒,就马上打电话test.php的。否则,休眠的剩余时间,使下一个test.php的将在从previous test.php的开头
Bash script:
$ cat test.sh
#!/bin/bash
while true
do
/home/user/public_html/website.com/test.php
sleep 5
done
Im trying to call it with cron:
* * * * * /home/user/public_html/website.com/test.sh
Where is my mistake? Does it matter where the .sh is located?
EDIT: I fixed all the problems from above. I also created this cron job to prevent multiple proccesses:
* * * * * flock -n /tmp/test.lock /home/user/public_html/website.com/test.sh
The problem now is that the bash executes the php every 5 seconds and this creates multiple proccesses like:
user 12:31 /home/user/public_html/website.com/test.php
user 12:31 /home/user/public_html/website.com/test.php
user 12:31 /home/user/public_html/website.com/test.php
How to control this? I need to change the bash script so it can detect if the PHP is being executed.
Is this correct:
#!/bin/bash
while true
if [ "$(pidof test.php)" ]
then
echo "Running"
else
do
/home/user/public_html/website.com/test.php &
sleep 5
fi
done
An alternative answer is to run you scripts for 12 times only, and put it in crontab
#!/bin/bash
for loop in 1 2 3 4 5 6 7 8 9 0 1 2; do
/home/user/public_html/website.com/test.php &
sleep 5
done
-- OR --
#!/bin/bash
loop=0
while [ $loop -lt 12 ]; do
/home/user/public_html/website.com/test.php &
sleep 5
loop=$(($loop+1))
done
--update--
If your goal is not to have more than one test.php, use this script: (Run it once only, do not putting it in the crontab):
#!/bin/bash
while true; do
begin=`date +%s`
/home/user/public_html/website.com/test.php
end=`date +%s`
if [ $(($end - $begin)) -lt 5 ]; then
sleep $(($begin + 5 - $end))
fi
done
Explanation: This script calls test.php, and wait for it to terminate (since it dose not have &
in the end). Then it measure the time: if it's already passed 5 seconds, it call test.php right away. otherwise, it sleeps for the remaining time so that next test.php will be called at the next 5 seconds starting from the beginning of the previous test.php
这篇关于bash脚本,执行PHP文件每5秒的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!