0

I'm creating a wordpress plugin that trades cryptocurrencies. It's a bot and I need to check the currency values every 10 seconds. In order to do that I need to run a PHP script through a cron-job.

I've tried to use wordpress cron jobs, but they are not reliable as I would like about the execution time.

I would like to ask: Is there a way to execute my cron job every 10 seconds on my server with CPanel? I tried with cronjob on the CPanel but they have a granularity of one minute and it's not enough Thank you in advance

0

First, you'll have to determine an average time to execute your job. Run it once every minute, five times. Use error_log('Start');, and error_log('End'); to mark the start time, and the end time. Take an average of all five results and round it up to full seconds. Prepare your job to execute its logic 6 times in a loop:

$avgExecTime = 3; //replace this with the average execution time from your tests
$sleepTime = 10 - $avgExecTime;
for($i = 0; $i = 5; $i++) {
    //
    // your logic goes here
    //
    if($i < 5)
        sleep($sleepTime);
}

Now, you can set your system cron to run this job every minute, forever.

Note: make sure that your job's average execution time is less than 10 seconds. If it is not, increase the frequency to 15 seconds, and modify your job accordingly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.