How do I create a cron job?

Procédure

What is a cron job?

A cron job is a mechanism for scheduling background tasks on your web hosting package. It allows commands or scripts to be run automatically at defined intervals.

Cron tasks are defined in a file called a cron file, which specifies which command should be executed and how often.

Cron uses a simple time format to define how often the task should run, such as minutes, hours, days, weeks and months. For example, to run a task every hour, you could set the task to run every 60 minutes.

Cron is a very useful tool for system administrators for automating various tasks, such as backing up data, updating software, sending emails, etc.

How do I create a cron job?

1. Connect to your hosting and click on"Cron tasks" in the "Database & PHP" section.

How do I create a cron job?

2. Enter the name of the task, the address of the script, the repetition interval and the days and times when the task will run.

How do I create a cron job?

How do I manage my cron jobs?

1. Connect to your hosting and click on"Cron tasks" in the "Database & PHP" section.

How do I create a cron job?

2. You can delete or pause cron jobs if necessary.


How do I create a cron job?

Example of cron jobs

  • Example 1: how to create a cron job that will run every day at 9.00am:
Script address : php /htdocs/my_script.php
Common parameters: Once a day
Execution time: Choose the execution time from the time drop-down window

  • example 2: how to create a cron job that will run every Wednesday at 6pm :
Script address : php /htdocs/my_script.php
Common parameters: Once a week
Day(s) of execution: Choose Wednesday in the "day of the week" drop-down window.
Run time : Choose "6 p.m" in the "Time" drop-down window.

The 'Execution time' variable is only taken into account if the task is repeated every 24 hours, otherwise the execution time will be the time the cron was added.

How do you log your cron job?

You can log your cron job by adding the following directive to it: &> /htdocs/cron.log

You can replace cron.log with the name of your choice. The command would then be as follows:

php /htdocs/monscript.php &> /htdocs/cron.log

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 click on"Terminal" in the "Software" section.

Access the cron file using the following command:

crontab -e

How do I create a cron job?

Once in the cron file, browse the file for the task you want 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?

How do I create a cron job?


Adding a cron job calling a script of the type tache.php?id=200

If the URL indicated in the"Script address" field is of the type: http: //mondomaine.com/tache.php?id=200, you will need to call the CURL function which will call your URL.

CURL and PHP are two different technologies that can be used to run cron tasks.

CURL is a command-line tool for transferring data from a URL. It can be used to make HTTP and HTTPS requests and is often used to download data from a website or to make API requests.

PHP is a programming language that can be used to develop web applications and perform tasks on the server. PHP can perform HTTP and HTTPS requests similar to CURL, but it can also execute code and manipulate data in more advanced ways.

When it comes to cron jobs, both can be used to run tasks in the background on a server. However, the difference between the two depends on what you want to achieve. If you just want to make an HTTP request or download data, CURL may be sufficient. If you want to perform more complex tasks, such as analysing data, manipulating databases or generating reports, PHP will probably be more suitable.

To do this, you can introduce a cron job such as the following:

curl /htdocs/tache.php?id=200

CURL can be used to perform a number of cron tasks, including :

  1. Updatingdata from an external API: CURL can be used to make API requests and download the updated data to your server.
  2. Backing up data to another server: CURL can be used to transfer data from your server to another server for backup.
  3. Check website availability: You can use CURL to check whether a website is online and accessible.
  4. Sending e-mail notifications: You can use CURL to send e-mail notifications from an automated script.
  5. Running scripts on another server : You can use CURL to trigger scripts on another server from your current server.

You can also use the "-f" option on your CURL cron job. This option allows you to define that the script will end with an error code if the request fails. This can be useful if you want to be informed if the request fails, for example if a website is inaccessible or if data cannot be downloaded. When a cron job is run with the -f option, if the request fails, the script will exit with an error code, which can be checked by the cron job itself or by another script monitoring the execution of the cron job. This can be used to trigger an additional action if the request fails, such as sending an email notification.

0 0 * * * curl -f https://www.example.com/data.json > /var/www/data.json

In this example, the cron job will run every day at midnight and download the data from https://www.example.com/data.json using CURL with the -f option. If the request fails, the script will end with an error code. Otherwise, the data will be saved in the /var/www/data.json file.

Conclusion

You are now able to create a cron job on your shared hosting with LWS. A cron job is a task that is activated automatically at set times.
Don't hesitate to share your comments and questions!

Rate this article :

3.1/5 | 10 opinion

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

1mn reading

How do I configure PHP on my hosting?

3mn reading

Customisation of error pages

0mn reading

How to access Apache / Php logs

2mn reading

How do you change the PHP version of your Joomla site to 7?


Questions sur l'article
Antoine Il y a 763 days
L'article n'est plus à jour avec la nouvelle version de la page sur LWSPanel V3.1 Pouvez-vous expliquer ce qui est attendu dans le champ Commande php /htdocs/monscript.php ? Avant l'url direct vers le script php fonctionnait, ce n'est plus le cas Merci
See the
1 answers
fabrice-LWS - Il y a 761 days
Bonjour, pour les tâches cron, il vous faudra insérer php /htdocs/dossier/monscript.php . Remplacez dossier/monscript.php par le chemin menant à votre script. Par exemple, si à l'époque, vous mettiez https://domaine.com/dossier/cron.php, il vous faudra désormais mettre php /htdocs/dossier/cron.php
Utile ?
Anasito Il y a 660 days
Quel est le nombre maximale des tâches cron selon les formules ?
See the
1 answers
Benjamin-LWS - Il y a 659 days

Bonjour,

Merci de votre retour.

Depuis le LWS Panel, et depuis toutes les formules, vous avez la possibilité d'éditer au maximum 5 tâches CRON.

Je vous remercie pour votre attention et reste à votre disposition pour toute autre question ou complément d'information.

Nous vous invitons à contacter notre service support à travers votre espace client, afin de vous apporter une aide si vous rencontrez des difficultés sur cette manipulation. (https://aide.lws.fr/a/268)

Cordialement, L'équipe LWS 

Utile ?

Ask the LWS team and its community a question