Rate this article :
This article was useful to you ?
Yes
No
Vous avez noté 0 étoile(s)
Procédure
The purpose of a cron job is to automatically run a script, a command or a programme at set times.
With"LWS cron job", you can schedule a script on your site to run at different times. For example, once a week, once a day, on the 8th of every month, etc.
A timeout of 240s is applied to these tasks, which means that if your script takes more than 4 minutes to run, it will be automatically shut down after 4 minutes.
1. Go to the Cron Task tool
First, connect to your LWS client panel
Then go to the management of the service for which you want to create a cron job.
Once in the management of your service, look for the "Database & PHP" block and click on "Cron tasks".
2. Configuring the frequency of the cron job
The first thing to configure for your cron job is its periodicity.
LWS offers a number of pre-settings that allow you to configure this in just 1 click.
To do this, simply use the first list at the top of the form and select one of the proposed parameters. This will automatically fill in the fields to match the periodicity indicated in the list you have selected.
If the periodicity you want to set up is not proposed in this first list, don't worry, we can configure each element.
For each item (minutes, hours, days, etc.) you can either enter the values manually, or use the list field to the right of each field.
So if, for example, I want my task to start every day at 12:27, all I have to do is select the number 27 in the list attached to the minute field and select 12:00 pm in the list attached to the hour field.
Tips / Advice
3. Configuring the command field
There are three ways of calling your script via the Cron Task tool:
The difference between these three options lies mainly in the way they execute your script.
A Wget or Curl call uses the HTTP protocol, which means that it will be subject to Apache configurations and restrictions such as the timeout, which can be relatively short.
The PHP call, on the other hand, will be free of the limitations we have already discussed with the other two methods. Think of the PHP call as going through an SSH connection.
So you might wonder which method to choose for setting up your Cron task. My answer would be that there are no real differences on basic scripts that run quickly. However, if your CRON task is fairly large, with a lot of actions performed and therefore a slightly longer execution time, the most suitable method will be a PHP call. Unlike Curl or Wget, the timeout will be longer (4 min).
We're going to look at how to set up a cron job for each of the calls mentioned above. We'll take as an example a simple site calling a root script.
cURL call
In a curl call using the HTTP protocol, we simply call the URL to our file as follows
curl https://www.mon-domaine.fr/cron.php
We may also need to send parameters to our script. To do this, simply add them as follows:
curl -d "param1=value1¶m2=value2" https://www.mon-domaine.fr/cron.php
We can then retrieve these parameter values in the code of our POST script.
Wget call
During a Wget call, which also uses the HTTP protocol, as with the cURL call, we will also call the URL of our script. This will give :
wget "https://www.mon-domaine.fr/cron.php"
If we need to pass parameters to the script, with Wget we will have no choice but to pass the parameters in GET. Here's how to write the command:
wget "https://www.mon-domaine.fr/cron.php?param1=value1¶m2=value2"
In the script, we can retrieve these two parameters using GET.
PHP call
In the case of a simple site, let's imagine that the file containing my script is called cron.php
All you need to do is enter the following command in the Cron Task tool:
php /htdocs/cron.php
Here, the PHP version used to run your script will be 8.0 by default.
If you wish to use a specific version of PHP, simply modify the call slightly. For example, if you want to use version 7.2 to run the script, this is the command called :
php72 /htdocs/cron.php
We may also need to pass an argument that will then be used in our script.
For example, let's imagine that the aim of our script is to send an email based on a criterion we define, such as the birthday. We might therefore want to pass this criterion in the call. Here's how to do it via the command line :
php72 /htdocs/cron.php type=anniversary
Special cases
There are also special cases depending on what you use for your site.
For example, in the case of a site created with the Cakephp framework, the CRON task configuration will be a little different because it will be done via a SHELL command called by the framework system.
Let's imagine that we have an e-commerce site created with the Cakephp framework and that we want to set up a script that will send an email to the best customer to offer them a promotional code.
We're going to create a Cake command that we'll call SendCP, which will contain the script that manages what we want to do. Here's how we'll configure the Cron task :
php72 /htdocs/Console/cake.php SendCP
It would take a long time to give an example of each case, but with what has been given here, you already have a good basis for using the Cron task tool.
Using the various examples, we now know how to write our command to ;
4. Creating a log file
We are now going to look at how to create a log file so that we can see what happened during the script called by Cron.
To do this, we'll use the example below for our CRON script.
Example of a PHP echo function for the log file
<?php /* So you don't have to worry about caching */ header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0"); header("Cache-Control: post-check=0, pre-check=0", false); header("Pragma: no-cache"); /* Write the current date to the file, for example: 2023/01/19 12:24:01 */ /* PHP_EOL allows you to skip a line in the file */ echo date('Y/m/d h:i:s').PHP_EOL; /* Write the sentence below in the log file */ echo 'Recording my Cron'.PHP_EOL.PHP_EOL; ?>
Rendering of the log file after launching the script via Cron Job
2023/01/19 02:57:01 Recording my Cron 2023/01/19 03:57:01 Recording my Cron 2023/01/19 04:57:01 Recording my Cron 2023/01/19 05:57:01 Recording my Cron 2023/01/19 06:57:01 Recording my Cron 2023/01/19 07:57:01 Recording my Cron 2023/01/19 08:57:01 Recording my Cron
What you put in your log file will therefore depend on you and what you want to track in your script. For example, it could be a list of actions with the result for each one.
Example of a cURL call with return in a log file
For a cURL call, here is the command to enter in the "Command" field:
curl https://www.mon-domaine.fr/cron.php >> /htdocs/logs/cron_curl.log
In this example, write feedback (echo PHP) will be sent to the cron_curl.log file in the logs folder. If the file or folder does not exist, it will be created automatically.
Example of a wget call with feedback in a log file
For a Wget call, here is the command to use:
wget -O - -q https://www.mon-domaine.fr/cron.php >> /htdocs/logs/cron_wget.log
In this example, write feedback (echo PHP) will be sent to the cron_wget.log file in the logs folder. If the file or folder does not exist, it will be created automatically.
Example of a PHP call with feedback in a log file
For a PHO call, here is the command to use:
php72 /htdocs/cron.php >> /htdocs/logs/cron_php.log
In this example, write feedback (echo PHP) will be sent to the cron_php.log file in the logs folder. If the file or folder does not exist, it will be created automatically.
Once you have created your cron tasks, you can find them directly in the listing that appears below the add form.
Here you'll find a list with the command recorded, the periodicity and the option of deleting a cron or pausing it.
Rate this article :
This article was useful to you ?
Yes
No
0mn reading
Multi-domain - Putting several domains on the same web hosting service
1mn reading
Is it possible to remove the RSpamD anti-spam function on shared hosting?
0mn reading
How can I view emails sent using the PHP mail function and blocked by SPAMASSASSIN?