What are cronjobs and how to configure them at Hipex?
A cronjob is a planned task which runs a command or script on a predetermined time. These tasks are commonly known as cronjobs. Cronjobs are essential in running repetitive tasks on a server. For example, think about tasks like optimizing images or emptying cache. To define these tasks as cronjobs, you no longer need to do this by hand, the tasks will be automatically executed at a defined frequency.
On the Hipex servers the tool crontab is used. This is a tool to add, change or remove cronjobs. At Hipex every SSH user has his own crontab available.
Crontab uses a syntax with which the frequency of the cronjobs is defined:
* * * * * │ │ │ │ │ │ │ │ │ └──────── Day of the week (0-7) │ │ │ └──────────── Month (1-12) │ │ └──────────────── Day of the month (0-31) │ └──────────────────── Hour (0-23) └──────────────────────── Minute (0-59)
To administer cronjobs you first need to log in using an SSH connection. Once logged in you are able to manage your cronjobs. Next we will explain to you how to view, add and remove cronjobs.
To add cronjobs we use the ‘edit’ option in crontab. To make use of the ‘edit’ option run the following command. It doesnt matter in what directory you currently are on the server.
You are now in crontab, which by default opens in the ‘vi editor’. Below a few handy commands:
- i - By pressing de ‘i’ key the editor changes to ‘insert mode’. Now changes in the text are possible.
- :q - Quit crontab
- :wq - Save changes and quit crontab
- :q! - Do not save changes and quit crontab
In our example we add a cronjob which we will optimize our images every night at 2:00 pm. This cronjob will look like this:
0 2 * * * /home/<user>/domains/<domainname>/public_html/usr/local/bin/magerun hipex:media:minify -d "since 1 day ago" --quality=85 media/wysiwyg/
By using variables we may shorten the above mentioned cronjob rules. These variables are configured at the top of the crontab. By using the PATH variable we avoid having to use the whole path description in the commandline of a cronjob. The PATH variable is shown by using the next command on the server:
Also the documentroot may be defined in a variable, docroot, for the same reason, avoiding typing the lengthy paths in our commands. Below we wrote the cronjob in full including both variables.
PATH=/home/<user>/.bin:/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin DOCROOT=/home/<user >/domains/<domainname>/public_html 0 2 * * * cd $DOCROOT && magerun hipex:media:minify -d "since 1 day ago" --quality=85 media/wysiwyg/
To view cronjobs we use the ‘list’ command. Using the command below enables you to view the actual configured rules in crontab:
This neatly includes the cronjob we just added.
PATH=/home/<user>/.bin:/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin DOCROOT=/home/<user>/domains/<domainname>/public_html 0 2 * * * cd $DOCROOT && magerun hipex:media:minify -d "since 1 day ago" --quality=85 media/wysiwyg/
Sometimes cronjobs need to be disabled. This is an easy task using the ‘edit’ option:
Also sometimes a specific cronjob needs to be disabled without fully removing it. Just "comment out" the cronjob by placing a hash (#) in front of that cronjob rule.
PATH=/home/<user>/.bin:/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin DOCROOT=/home/<user>/domains/<domainname>/public_html # 0 2 * * * cd $DOCROOT && magerun hipex:media:minify -d "since 1 day ago" --quality=85 media/wysiwyg/
If a cronjob has become obsolete, it can be fully removed by deleting the whole rule from the crontab.