Prestashop logo, Visit home page

Manage scheduled tasks (cronjobs)

-

A cron job is a scheduled task. Scheduled tasks are managed by a process called cron. The cron process follows what is called a crontab (short for cron table). There is a system crontab and separate crontabs for each user. These steps will teach you how to add scheduled tasks to a user-based crontab.

Comment ajouter une tâche cron ?

Hosting_terminal.png

Go to the TERMINAL tab of the environment of your choice. There you will find a link to open the terminal and the credentials you will need to authenticate. With the terminal open, type this command followed by the Enter key:

crontab -e -u webapp

You will now see the nano editor which has opened the crontab for the currently logged in user, in this case webapp. The content of the editor will look like this:

# Edit this file to introduce tasks to be run by cron.
#
# Each task to run has to be defined through a single line
# indicating with different fields when the task will be run
# and what command to run for the task
#
# To define the time you can provide concrete values for
# minute (m), hour (h), day of month (dom), month (mon),
# and day of week (dow) or use '*' in these fields (for 'any').#
# Notice that tasks will be started based on the cron's system
# daemon's notion of time and timezones.
#
# Output of the crontab jobs (including errors) is sent through
# email to the user the crontab file belongs to (unless redirected).
#
# For example, you can run a backup of all your user accounts
# at 5 a.m every week with:
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
#
# For more information see the manual pages of crontab(5) and cron(8)
#
# m h  dom mon dow   command

Warning: The text above is a default explanation of the cron package.

The text in the editor already gives you an explanation of how things work, along with an example:

0 5 * * 1 tar -zcf /var/backups/home.tgz /home/

In this example, you'll notice the first five characters separated by a white space (a single space or tab): these determine the interval at which the task will run, where an asterisk acts as a wildcard. Days of the week range from 0 on Sunday to 6 on Saturday. The five fields from left to right indicate:

  • minutes
  • hours
  • day of the month
  • month
  • day of the week

This would mean that the example above will run at:

  • 0 minute
  • 5 hours
  • every day of the month
  • every month
  • 1st day of the week: Monday

In summary: at 5:00 a.m. every Monday.

Just after the interval you can see the command that will be executed by the cron:

tar -zcf /var/backups/home.tgz /home/

 

Now let's add our own cron job. You will need to consider a few things:

  1. PS Platform uses environment variables that must be loaded into memory before executing tasks.
  2. You will need to ensure that your scheduled tasks run in the correct directory.

Both of these can be resolved by chaining commands in the crontab file using a double ampersand &&. The double ampersand will execute the next command only if the first command was successful, that is, if it exited with exit code 0. So, to make sure we have the environment variables loaded and that we are in the correct directory before starting execution, our command will start like this:

. /.victhorious/conf/shell/envvars && cd /vol/site/current/ && #TODO: add actual command

Now that we have the environment variables loaded and we are in our root folder, we can complete the command with what we plan to run. Let's say we want to schedule the Craft CMS Feed Me import with cron.

Running Feed Me commands as command line arguments is the best option, but there are two commands to do this successfully:

  1. Queue new data first.
  2. Process queued data.

So we need to chain two additional commands together. This time we'll use the semicolon (;) to do it, which will make our command look like this:

. /.victhorious/conf/shell/envvars && cd /vol/site/current/ && php craft feed-me/feeds/queue 1 --continue-on-error; php craft queue/run

 

Advice :
Unlike the double ampersand, the semicolon will ensure that all commands are executed regardless of their exit code!

 

Now we still need to build our time interval part. Let's say we want to run this import every 15 minutes, then we would have this line from crontab:

*/15 * * * * . /.victhorious/conf/shell/envvars && cd /vol/site/current/ && php craft feed-me/feeds/queue 1 --continue-on-error; php craft queue/run

The */15 could be read as 'all the minutes which can be divided by the number 15', which are:

  • minute 0
  • minute 15
  • minute 30
  • minute 45

In summary: every 15 minutes.

Advice :

If you are having trouble constructing the correct time syntax for a desired interval, you can use crontab guru to help you.

 

Since most commands usually produce some output, we will want to suppress this output to avoid unnecessary garbage accumulating in the form of user alerts about cron jobs.

The way to do this is to redirect standard output and standard error to /dev/null like this :

*/15 * * * * . /.victhorious/conf/shell/envvars && cd /vol/site/current/ && php craft feed-me/feeds/queue 1 --continue-on-error > /dev/null 2>&1; php craft queue/run > /dev/null 2>&1

Advice :

Adding > /dev/null 2>&1 behind each command will send output to /dev/null, while 2>&1 will also redirect stderr to stdout, making it end up in /dev/nullas well. If you replace > /dev/null with >> /vol/site/cron.log, you will be able to log the output to the /vol/site/cron.log file on disk.

 

Attention :

The percentage character (%) has a special meaning in crontab syntax. We're not going to go into detail here, but be aware that the % sign must be escaped if you want to use it. So if you want to log a formatted date every time cron runs, you should do something like date date +\%D-\%T.

Now save the crontab changes by pressing CTRL+X, followed by typing Y and hitting Enter.

You can test your cron with:

Crontab.guru - The cron schedule expression editor

 

Cron examples with PrestaShop URL call with curl

crontab -e -u webapp
00 01 * * * curl -L -s -o /dev/null "<https://mywebsite.fr/modules/gsitemap/gsitemap-cron.php?token=XXXXXXXXX&id_shop=1>"
00 02 * * * curl -L -s -o /dev/null "<https://mywebsite.fr/admin/index.php?controller=AdminSearch&action=searchCron&ajax=1&full=1&token=XXXXXXXXX&id_shop=1>"

Same as:

00 01 * * * wget -q -O /dev/null "<https://mywebsite.fr/modules/gsitemap/gsitemap-cron.php?token=XXXXXXXXX&id_shop=1>"
00 02 * * * wget -q -O /dev/null "<https://mywebsite.fr/admin/index.php?controller=AdminSearch&action=searchCron&ajax=1&full=1&token=XXXXXXXX&id_shop=1>"

 

View installed scheduled tasks

To quickly view the list of scheduled tasks, you can simply enter:

#root
crontab -l

#webapp
crontab -l -u webapp

This command will display the crontab file for your user on screen.

Share

Was the article helpful?