How to create CRON tasks for PHP

24 de junio, 2021 @ 12:23 — 0 comentariosDownload PDF

Cron is a task manager, which executes at regular intervals the tasks that we have configured. We can make the tasks run, for example, every minute, day, week or month.

Cron task scheduler

Some web sites need to perform periodic maintenance tasks and require configuring a CRON for it. To do this the first thing to do is to access our Hosting Panel.

We will find in the Utilities section, the CRON Task Scheduler icon.

If we access it, we will see a simple form to schedule our task.

Scheduling

In the schedule we define the frequency with which the command is to be executed:

  • Every 5 minutes
  • Every 15 minutes
  • Every 30 minutes
  • Every hour
  • Once a day
  • Once a week
  • Once a month
  • Advanced...

Command

The command is what we want to execute and there are usually 2 ways to execute functions on the web:

1 - Direct request to the website

Our web site may require us to visit a specific web site (for example: http://midominio123.com/script.php ), in this case we will use the wget command followed by the URL we want to execute, like this:

wget --spider http://midominio123.com/script.php

If you do not want to generate any output, type the following at the end of the command: >/dev/null 2>&1

wget --spider http://midominio123.com/script.php > /dev/null 2>&1

* In this case the PHP configuration of the website will be applied.

2 - Execution by script

We can also execute scripts, which are not on the web, by running them directly:

php /home/user/www/script.php &>/dev/null

* In this case the PHP configuration of etc/cron/php.ini is applied.

When you are done, click on the Submit button to program the command.