Running PHP scripts automatically through crontab on Linux

Views: 4.947
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." For weekdays, 0 to 6 means "Sunday to Saturday". Note that the number 7 can also be used. In this case, just like the 0 number, 7 is equivalent to “Sunday” day. Instead of these values, you can enter * (asterisk) to specify a constant execution. For example, if the days of the month field contains *, the related command runs every month.

You can also enter fill intervals by separating start and end numbers with - (hyphen). For example, if in the hours field you enter 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 into the log.txt file, in the / home / dirceu / directory, at 22 hours and 30 minutes, on days 3 and 14, every month and every day. of the week. Notice the line “#scheduled task”. This is a comment. Enter # and anything you type on the line will not be considered by cron. This 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 PHP scripts, simply enter the php / path / do / script command in your cron. However, as each server has different configurations, this command is not always executed correctly, especially in more complex applications that use PHP's “magic” __autoload feature.

A KingHost For example, it has a very nice, fast and practical interface to manage the tasks scheduled by the control panel itself (despite charging $ 5,00 per task .. nonsense…). I have already configured several tasks and had no difficulty to do so.

Other servers, such as UOL Host, liberam acesso SSH para os clientes configurarem suas crons manualmente através de um terminal SSH de sua preferência (Recomendo o

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

where the wget command reads the requested file, saves a copy on your server, and displays the contents on the screen. But in this case, we don't want the message on the screen let alone a copy of the php script not true? So let's go to the ultimate solution:

Where the -q command inhibits the wget generated message and the –delete-after command deletes the copy of the created file.