Try this:
#!/bin/bash
while true; do
sleep 5 &
php /home/user/www/run.php &
wait
done
wait
(with no arguments) waits for all background jobs to complete, so the loop will block until the longer of the sleep and php processes completes.
Demonstrating the SECONDS variable
rand() { echo $(( RANDOM % $1 )); }
for i in {1..10}; do
start=$SECONDS
r=$(rand 10)
echo "iteration $i, sleeping for $r seconds"
sleep $r
end=$SECONDS
if ((end - start < 5)); then
n=$((5 - (end - start)))
echo "sleep for $n seconds"
sleep $n
fi
done
# or more simply by assigning to SECONDS
for i in {1..10}; do
SECONDS=0
r=$(rand 10)
echo "iteration $i, sleeping for $r seconds"
sleep $r
duration=$SECONDS
if ((duration < 5)); then
n=$((5 - duration))
echo "sleep for $n seconds"
sleep $n
fi
done
Certainly more complex than usingwait
The best thing would be to configure crontab to keep the execution or another program installed as a service.
The problem with contrab is that the minimum execution is every 1 minute. Therefore, you should create a script that executes every 5 seconds no more than 12 times. (12 x 5 seconds = 60 seconds)
Kill the process and re-run it with crontab.
sript.sh
#!/bin/bash
# Do not run 12 times because this will same time as next crontab execution
for i in 1 2 3 4 5 6 7 8 9 11
do
php /home/user/www/run.php
sleep 5
done
On crontab
* * * * * /path/to/script.sh
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Find the answer in similar questions on our website.
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
PHP (from the English Hypertext Preprocessor - hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites.
The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
https://www.php.net/
Welcome to the Q&A site for web developers. Here you can ask a question about the problem you are facing and get answers from other experts. We have created a user-friendly interface so that you can quickly and free of charge ask a question about a web programming problem. We also invite other experts to join our community and help other members who ask questions. In addition, you can use our search for questions with a solution.
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.