0

I can create a bash script that runs a PHP file over and over with a 3 second delay like this:

php -q /home/script.php
sleep 3
php -q /home/script.php
sleep 3
php -q /home/script.php

But I'm looking for a better way to do this, so I don't have to create a file with hundreds of thousands of lines and then check to see when it's done so I can just restart it.

Is there any way to create a loop that runs a PHP file and once it's done, it just does it again - for an infinite amount of time (with a 3 second delay between each run)?

1
  • I'm curious as to what task would need to be run that often. There's a chance that taking action based on event happening, instead of checking every 3 seconds to see if that event happened, may be preferred. Commented Feb 7, 2020 at 20:21

2 Answers 2

1

Using Cronjob

What Are Cron Jobs? https://www.hostgator.com/help/article/what-are-cron-jobs

A cron job is the scheduled task itself. Cron jobs can be very useful to automate repetitive tasks.

Some useful tool for cronjobs: https://crontab.guru/

An example: "run a script every 1 minute"

*/1 * * * * bin/php /path/to-your/script.php

Looping

In case you really need to repeat a task each X seconds, you could write a while loop for that:

#!/bin/bash
while true; do
  # Do something
  sleep 3;
done
Sign up to request clarification or add additional context in comments.

3 Comments

True that. In case you want to go for "seconds" I would do a while loop with some sleep inside... but is way more common to repeat certain task after X minutes, more than X seconds...
Yeah, but the OP wants seconds, so cron doesn't seem a good option here.
I edited my answer to display the two options that came to my mind so far.
1

You may consider writing a while loop:

while true
do
  php -q /home/script.php
  sleep 3 
done

1 Comment

I definitely prefer cron as a solution, but you will learn that cron only has minute resolution.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.