Husain
Husain

Reputation: 129

Running a php script as a cron job every 10 seconds

This question may seem repetitive, as there are many threads around with the same subject, but thing is that most solutions seems to be linked with terminal coding, which i'm not comfortable with. The problem is simple i have a php script that needs to be executed very 10 seconds. Cron job in cpanel allows only upto 1 minute. What's the workaround to let cron work every 10 seconds ?

Upvotes: 3

Views: 7713

Answers (4)

juanbits
juanbits

Reputation: 347

You can use the GUI Cpanel, select "once per minute" an try with something like the next command:

/path/to/bin/php /path/to/script.php; sleep 10; /path/to/bin/php /path/to/script.php; sleep 10; /path/to/bin/php /path/to/script.php; sleep 10; /path/to/bin/php /path/to/script.php; sleep 10; /path/to/bin/php /path/to/script.php; sleep 10; /path/to/bin/php /path/to/script.php

Upvotes: 1

Ahmed Numaan
Ahmed Numaan

Reputation: 1062

Let the cron job run after every minute and in your php script the following code example might help you out. I have used counter limit to 6 because this script will run after every ten seconds and six times in one minute.

<?php
for($i=0;$i<6;$i++){
    sleep(10);
    task();
}

function task(){

}

Upvotes: 8

Gijo Varghese
Gijo Varghese

Reputation: 11780

CRON jobs are the standard way to run some tasks periodically. Setting cron jobs require access to the terminal. However, some shared hosting providers don't provide this and you need to set it up through their interface.

If you hosting don't provide this you can third-party services that will call you url every 'X' seconds.

Here are few of them:

You can Google for more

Note: You can have publically expose the PHP file as an url

Upvotes: 1

vuliad
vuliad

Reputation: 2152

You can use simple bash-script like

#!/bin/sh while [ true ] do php script.php sleep 10 done

Upvotes: 1

Related Questions