Sign in Registration
ruen

Cron or run on hits - which is better, a function to run a script on a schedule

Sometimes you need to launch a script in order for it to perform certain tasks. This can be very important for the site to work. They are also often asked what cron is, how it differs from running on hits, and what is better. This article will briefly discuss these technologies.

run-cron-or-hits

Why would you want to run code on schedule ? There are many examples:

  • clearing the contents of the folder;
  • sending emails and notifications;
  • delete users;
  • creating backups;
  • checking a site for viruses ;
  • background optimizations for database records;
  • reporting;
  • checking for updates, etc.

How to make scheduled script run ? This can be done in different ways, for example, using cron or running on hits. The launch on hits occurs when a user visits the site, but this is an unreliable way of completing tasks. Another way is to use cron - this is a special program on the server that launches the execution of the necessary script, just add a task for it.

Running on hits is unreliable for the reason that site traffic is not guaranteed - it is not known when someone will visit it. Because of this, the time accuracy of the tasks cannot be ensured - the script will be executed at an arbitrary time. Usually launching on hits is very easy. In the code of the site, the code is placed in the right place that checks the time, if it has expired, the execution of another script is launched.

A simple example - a sample function to run a script on a schedule :

  // function for launching tasks on hits (get_time_prev and set_time_prev are conditional functions)
function run_tab () {
// get the current and previous time
$ time = time ();
$ time_prev = get_time_prev ();

// if more than 5 minutes have passed since the last launch, start again
if ($ time> ($ time_prev + 300)) {
// execute the required code
...

// update the time when it was last launched
set_time_prev ($ time);
}
}  

Running a script via cron is more difficult, but it allows you to achieve accurate job execution. All tasks are started by the server itself at exactly a certain time. However, for this you need to be able to add tasks for it, and not all users are able to do this. Therefore, for convenience, popular CMS implements a special mechanism for launching cron in one click, and there are also settings for its launch time, for example - Drupal CMS .

Thus, we have considered the technologies for launching functions on a schedule. Which is better - run on cron or run on hits ? There is no definite answer to this question, in some cases the first option will be useful, in other cases the second.

Comments (0)
For commenting sign in or register.
Оставить заявку

Latest articles

Popular sections

Eqsash (Tools)

Android app - VK LAST USER ID, отучитель от зависимости и т.д.:
Available on Google Play

Amessage (Communication)

Login to the web version
Android app:
Available on Google Play

Share this

Subscribe to

YouTube

Books

IT notes - In simple language about the most necessary things (HTML, CSS, JavaScript, PHP, databases, Drupal, Bitrix, SEO, domains, security and more), PDF, 500 p.