How do I create a cron job from the LWS Panel?

Procédure

What is a cron job?

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.

Cron is a very useful tool for system administrators to automate various tasks, such as backing up data, updating software,sending emails, 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.

How do I create a cron job?

Access the Cron Task tool

First, you need to log in to your LWS account and access the shared hosting 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".

How do I create a cron job from the LWS Panel?

Configuring the frequency of the cron job

The first thing to configure for your cron job is its periodicity.

At this level, LWS offers pre-settings in its tool so that you can configure this in 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.

How do I create a cron job from the LWS Panel?

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 (noon) in the list attached to the hour field.

Tips / Advice

  • Except in very special cases, we don't recommend setting up a cron job to run every minute so as not to saturate the server and therefore reduce the performance of your service.
  • Prefer tasks that run at night and at specific times (such as 3.47am ) to avoid running your tasks at the same time as possible automatic tasks, as some CMS or tools used on your site may do.

Configuring the command field

There are three ways of calling your script via the Cron Task tool:

  • PHP call
  • Call wget
  • cURL call

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.

ThePHP call, on the other hand, will be free from the limitations that we discussed with the other two methods. Think of the PHP call as going through an SSH connection.

You might therefore wonder which method to choose for setting up your Cron task. Our answer is that there are no real differences for 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 aPHP 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

When making a curl call using the HTTP protocol, we simply call the URL to our file as follows

curl -s -H "Cache-Control: no-cache" "https://www.mon-domaine.fr/cron.php" > /dev/null

We may also need to send parameters to our script. To do this, simply add them as follows:

curl -d "param1=value1&param2=value2" -s -H "Cache-Control: no-cache" "https://www.mon-domaine.fr/cron.php" > /dev/null

This will allow us to retrieve these parameter values in the code of our POST script.

Option Role
-s Silent mode (no progress bar).
-H "Cache-Control: no-cache Force server-side no-cache.
> /dev/null Redirects output to save nothing.

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 --no-cache --output-document=/dev/null --header="Cache-Control: no-cache" "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 --no-cache --output-document=/dev/null --header="Cache-Control: no-cache" "https://www.mon-domaine.fr/cron.php?param1=value1&param2=value2"

In the script, we can retrieve these two parameters in GET.

Option Role
--no-cache Avoids the use of intermediate caches (useful with certain proxies).
--output-document=/dev/null Stores nothing locally.
--header="Cache-Control: no-cache" Sends an HTTP header to avoid server-side caching.

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 we have an e-commerce site created using the Cakephp framework and we want to set up a script that will send an email to the best customer offering 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 :

  • simply run our script at the chosen intervals
  • run our script by adding parameters and at the chosen intervals

Creating a log file

We're 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 -s -H "Cache-Control: no-cache" "https://www.mon-domaine.fr/cron.php" > /htdocs/logs/cron_curl.log 2>&1

In this example, writebacks (echo PHP) will be made to the cron_curl.log file in the logs folder. If the file or folder does not exist, it will be created automatically.

Option Role
-s Silent mode (no progress bar).
-H "Cache-Control: no-cache" Force server-side no-cache.
> /htdocs/logs/cron_curl.log redirects standard output (stdout) to cron_curl.log.
2>&1 also redirects errors (stderr) to the same file.

Example wget call with return to a log file

For a Wget call, here is the command to use:

wget -O - -q --no-cache --header="Cache-Control: no-cache" "https://www.mon-domaine.fr/cron.php" > /htdocs/logs/cron_wget.log 2>&1

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.

Option Role
--no-cache Avoids the use of intermediate caches (useful with some proxies).
--header="Cache-Control: no-cache" Sends an HTTP header to avoid server-side caching.
--post-data="..." adds a custom HTTP header.
> /htdocs/logs/cron_wget.log redirects standard output (stdout) to cron_wget.log.
2>&1 also redirects errors (stderr) to the same file.

Example of a PHP call with return to 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.

How do I manage my cron tasks?

Viewing my created cron tasks

Once you have created your cron tasks, you can find them directly in the listing that appears below the add form.

How do I create a cron job from the LWS Panel?

Here you'll find a list with the command recorded, the periodicity and the option to delete a cron job or pause it.

How do I create a cron job from the LWS Panel?

How do I modify a cron job?

If you have an LWS Starter package or higher, you can modify the cron job for your package via the Web Terminal accessible from your package administration.

To do this, connect to your hosting and access the shared hosting for which you want to modify a cron job.

Click on"Terminal" in the "Software" section.

How do I create a cron job from the LWS Panel?

Access the cron file using the following command:

crontab -e

How do I create a cron job from the LWS Panel?

Once in the cron file, browse the file for the task you wish to modify. Each line in the file represents a separate task. Then modify the cron job, making sure that the syntax remains correct. Then save it using the keyboard shortcut [CTRL] +X then"Y". Then press [Enter].

You can then check whether the cron job has been correctly modified by using the following command:

crontab -l

How do I create a cron job from the LWS Panel?

Using Cron Tasks with Cloudflare DNS

Why don't my crons work with CloudFlare?

You have linked your domain to CloudFlare using the latter's DNS and you want to configure a cron job from your customer area in the associated section, but the job only executes once without repeating itself at the time interval you wanted. This problem is known, CloudFlare blocks the execution of crons sent from our services. To overcome this problem, here's a workaround.

Running a cron job using CloudFlare

The cron tasks configured from the customer area don't work if you use CloudFlare DNS. To overcome this problem, please follow this procedure:

  1. Create a sub-domain (for example: cron.domain.ext) from your client space by following this documentation
  2. Place the file to be executed by your cron job in the folder bearing the name of your sub-domain on your hosting's FTP space.
  3. Create your cron job from the customer area by following the instructions above in this documentation.
  4. Connect to your CloudFlare account linked to your domain name.

In the "DNS" section, add a CNAME record as shown in the following example:

How do I create a cron job from the LWS Panel?

Replace"cron" with the name of your sub-domain and"domain.ext" with your domain name.

Make sure that the cloud is grey and not orange. This tells CloudFlare that you don't want to use its services for this sub-domain.

You can then click on the"Add Record" button.

Conclusion

You now know how to : - Understand the purpose and operation of a cron job 🤖

  • Configure and run scripts automatically using LWS cron task ⏱️
  • Access the Cron Task tool and configure periodicity
  • Using the different calling methods (PHP, wget, cURL) for your scripts 🌐
  • Use log files to monitor your cron tasks 📝
  • Manage, modify, and view your created cron tasks 🛠️
  • Using cron tasks with Cloudflare DNS to work around runtime issues 🛡️

We hope this article has provided you with all the keys to mastering cron tasks and automating your system tasks efficiently.

If you have any questions, feedback or tips to share, don't hesitate to leave them as a comment. Thanks for reading and see you soon for more practical advice! 🙏💬

Rate this article :

This article was useful to you ?

Article utileYes

Article non utileNo

Vous souhaitez nous laisser un commentaire concernant cet article ?

Si cela concerne une erreur dans la documentation ou un manque d'informations, n'hésitez pas à nous en faire part depuis le formulaire.

Pour toute question non liée à cette documentation ou problème technique sur l'un de vos services, contactez le support commercial ou le support technique

MerciMerci ! N'hésitez pas à poser des questions sur nos documentations si vous souhaitez plus d'informations et nous aider à les améliorer.


Vous avez noté 0 étoile(s)

Similar articles

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?


Ask the LWS team and its community a question