Running PHP scripts automatically through crontab on Linux

Views: 5.753
Reading Time: 3 minutes

Introduction

Suppose you have developed a system for a company and at one point that company has asked you to have a system that needs to send certain emails to its customers every Monday, and your system must perform automatic backups every Friday, at 23: 00.

And now? How do we do it?

What is cron

Well, this is the solution for your system! Cron can be interpreted as a service on Linux operating systems that allows you to schedule script and command execution at certain scheduled times.

To execute the tasks, cron uses a kind of table known as crontab. The crontab file is usually located in the / etc directory, but it can also be in a directory that creates a crontab for each system user (usually in / var / spool / cron /), it all depends on the operating system settings you use.

Using cron

The first step is to open crontab. To do this you can use system text editors or via the command crontab -e to edit your user's unique file. In this case, editing is done as if you were using vi.

Crontab has the following format:
[minutes] [hours] [days of the month] [month] [days of the week] [user] [command]

The completion of each field is as follows:
- Minutes: enter numbers from 0 to 59;
- Hours: enter numbers from 0 to 23;
- Days of the month: enter numbers from 0 to 31;
- Month: enter numbers from 1 to 12;
- Days of the week: enter numbers from 0 to 7;
- User: It is the user who will execute the command (it is not necessary to specify it if the user's own file is used);
- Command: the task that must be performed.

crontab

Note that the order of these values ​​indicates the corresponding field name. For example, in the month field, 1 to 12 means “January to December”. In the case of weekdays, 0 to 6 means “Sunday to Saturday”. Note that the number 7 can also be used. In this case, like the number 0, the 7 is equivalent to the day of "Sunday". In place of these values, you can enter * (asterisk) to specify constant execution. For example, if the days of the month field contains *, the related command will be executed every month.

You can also enter intervals when filling in, separating the start and end numbers with - (hyphen). For example, if in the hours field it is informing 2-5, the related command will be executed at 2, 3, 4 and 5 hours.

What if the command has to be run at 2 hours, between 15 and 18 hours, and at 22 hours? Just enter 2,15-18,22. In these cases, you separate the parameters with commas.

Let's take an example:
#scheduled task
30 22 3,14 * * echo “Don't panic”> /home/dirceu/log.txt

In this example, the phrase “Don't panic” is inserted in the log.txt file, inside the / home / dirceu / directory, at 22:30 pm, on the 3rd and 14th, every month and every day of the week. Notice the line “#Tasked Task”. It is a comment. Type # and anything entered on the line will not be considered by cron. It is a useful feature for entering descriptions when you have multiple tasks to perform.

Crontab commands

To access crontab, simply type this name into a terminal followed by a parameter. Here is the list of available parameters:
crontab -e: As already informed, it is used to edit the current crontab file and create one if it does not exist;
crontab -l: This command shows the current content of crontab;
crontab -r: remove the current crontab file.

Running PHP Scripts on Web Servers via cron

Now that you have learned how to use cron, how can we run our application scripts?

To run the PHP scripts, simply enter the command php / path / do / script in your cron. However, as each server has different configurations, this command is not always executed correctly, especially in more complex applications that use the "magic" __autoload feature of PHP.

A KingHost for example, it has a very cool, fast and practical interface to manage the tasks scheduled by the control panel itself (despite charging R $ 5,00 per task ... absurd ...). I have already configured several tasks and I had no difficulty in doing so.

Other servers, such as UOL Host, allow SSH access for customers to configure their crons manually through an SSH terminal of their choice (I recommend the

1
wget www.seusite.com.br/seu_arquivo_cron.php

onde o comando wget faz a leitura do arquivo solicitado, salva uma cópia no seu servidor e exibe o conteúdo na tela. Porém, neste caso, não queremos a mensagem na tela e muito menos uma cópia do script php não é verdade? Então, vamos para a solução definitiva:

Onde o comando -q inibe a mensagem gerada pelo wget e o comando –delete-after exclui a cópia do arquivo criado.