Best Way to Periodically Execute a PHP Script

Best way to periodically execute a PHP script?

You can use an online cron service to essentially pretend like you have cron access.

Create php file with contents you would like executed

Free Cron Online Website

Set up your free online cron to execute that file every x minutes.

Best way to periodically execute a PHP script?

You can use an online cron service to essentially pretend like you have cron access.

Create php file with contents you would like executed

Free Cron Online Website

Set up your free online cron to execute that file every x minutes.

How to run a PHP script continuously?

For this particular issue cron jobs have been invented. Cron jobs are timed jobs that can for example execute a PHP script.

You could set up a cron job to check which user should receive his/her sms every hour. Depending on your operating system you can set up these cron jobs. For linux distrubutions there are tons of guides on how to set this up.

From Wikipedia:

The software utility Cron is a time-based job scheduler in Unix-like computer operating systems. People who set up and maintain software environments use cron to schedule jobs (commands or shell scripts) to run periodically at fixed times, dates, or intervals. It typically automates system maintenance or administration—though its general-purpose nature makes it useful for things like downloading files from the Internet and downloading email at regular intervals. The origin of the name cron is from the Greek word for time, χρόνος (chronos). (Ken Thompson, author of cron, has confirmed this in a private communication with Brian Kernighan.)

I have added a resource explaining how to use cron jobs.

An alternative method is to keep a PHP script running in the background:

// Keep executing even if you close your browser
ignore_user_abort(true);

// Execute for an unlimited timespan
set_time_limit(0);

// Loop infinitely
// If you create a file called stop.txt,
// The script will stop executing
while (!file_exists('stop.txt')) {
// Retrieve user data and sens sms messages

// Wait for an hour
sleep(3600);
}

Update

ignore_user_abort(true);
set_time_limit(0);
$data = file_get_contents('filename.txt');
while (!file_exists('stop.txt')) {
// Add 1 to $data
$data = $data+1;
// Update file
file_put_contents('filename.txt', $data);

// Wait 4 seconds
sleep(4);
}

To stop executing create a file called stop.txt

Resources

  • About Cron jobs

How to periodically execute a PHP script/plugin on wordpress?

Use a cron job. There are tons of tutorials out there on how to do that. There is even a Wordpress Cron Job scheduler plugin (this link is only for reference, I'm not endorsing it specifically). You can find it here. Note that you will need administrative rights on the server to execute cron jobs.

How can I make my PHP script run at a certain time everyday?

What you want is called a cron job and is already covered here:

PHP: running scheduled jobs (cron jobs)

how to execute a PHP code periodically using AJAX?

Do you know what exactly is AJAX?

It looks like you will be using a library for this, so take a read here: http://api.jquery.com/jquery.ajax/

If you are not planning to use jQuery, you could check this XMLHttpRequest project that is cross-browser: https://github.com/ilinsky/xmlhttprequest

Make use of AJAX periodically using a setInterval of 5 seconds where you would make a call to a PHP file you have in your server that would run the code you specified.

Take the following example:

index.html

setInterval(function() {
$.get("file.php", function (data) {
console.log('Good to go, data was loaded');
console.log(data);
});
}, 5000);

file.php

<?php
$result = mysqli_query($con,"SELECT * FROM jqcalendar ORDER BY id DESC LIMIT 0, 1");
while($row = mysqli_fetch_array($result)){
echo "a new event was added";
}
?>


Related Topics



Leave a reply



Submit