What are CRON JOBS and how to use them with PHP

Asked

Viewed 10,833 times

4

Recently asked this question, but I found it kind of vulnerable to use the codes that were answered there, summarizing the best answer: Creates a Cookie with a Time and hides the button, after this time runs out, the button returns. (If any daredevil clean up the cookies it can use the system again).

Well I’d like to do the same using Cron but so far I don’t understand what they are, and how to use them to run a script every certain time, taking a look here in the OS, I found this topic, but for me it is in "Japanese", I would like to know a simple example of cron, in the case with this reasoning:

  1. Check the number of people sent, (first question link to understand).
  2. Check whether the amount passed the Limits in the case I used in the query LIMIT 10.
  3. Run the code, and hide the button (this with javascript I know how to do).
  4. When finished executing the code shows a message "Go back to x minutes"
  5. After x minutes the reactive button.

I hope you explain what it is cron and a basic example of how to do what I want, understood?

2 answers

12

Cron is a task scheduler present on Unix systems. It only does this: schedules tasks to be performed at regular intervals.

In answer to your question they gave you this code:

59 23 * * * php -f /home/u844214382/cronjobs/nomedoarquivo.php

It can be translated as:

Every day, at 23 hours, in the minute 59, execute the command

php -f /home/u844214382/cronjobs/nomedoarquivo.php

You can see how to create these and other combinations of commands using this site: https://crontab-generator.org/

Cron is a generic system tool. A only relationship with the PHP if it is due to the fact that it is able to fire scripts on the system, then you can use it to run PHP scripts on the command line as well, as the example indicates.

To use a metaphor:

Cron is an alarm clock. At the interval you send it will ring and poke his script to wake him up. From there the script turns around alone to accomplish the tasks that have to be done.

Then the questions you ask from 1 to 5 can be solved in pure PHP, taking into account that it will be executed on the command line (and not in a request from a browser), having Cron only as scheduler and trigger of tasks.

  • Opa, already gave a lighted here, let’s see what I can do with your answer. Have two, which I get, mark as solved.

6

Cron jobs has nothing to do with your problem, setting up a cron job on the server, simply makes a script run every X time.

This time validation can only be done in the client or javascript, to have a control over it would be necessary, a column in the bank with value booleano to the button, and a column like Date, comparing the time the button was triggered and the time the user entered the page again.

Another alternative would be to limit time on sessão, but it would not be so precise until pq setar sessões consume from your server, so pq consume in several places, can consume from only one? Session time

remember that you are treating multiple users on a given page, you will need to have control under each one individually.

  • Oops, you already lit up here, let’s see what I can do with your answer.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.