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.
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.
Latest articles
- 03.04.24IT / Уроки PHP Уроки простыми словами. Урок 3. Все операторы PHP с примерами, с выводом работы кода на экран.
- 02.04.24IT / Уроки PHP Уроки простыми словами. Урок 2. Типы данных в PHP с примерами.
- 02.04.24IT / Уроки PHP Уроки простыми словами. Урок 1. Коротко о языке веб-программирования PHP. Основы синтаксиса.
- 09.11.23IT / Database Errors when migrating from MySQL 5.6 to 5.7 and how to fix them - database dump import failed with an error or INSERT does not work. Disabling STRICT_TRANS_TABLES strict mode or using IGNORE
- 08.07.22IT / Misc Convert office files DOC, DOCX, DOCM, RTF to DOCX, DOCM, DOC, RTF, PDF, HTML, XML, TXT formats without loss and markup changes