/
Pack Hosting Panel

Cronjobs

What are cronjobs and how to configure them at Hipex?


What are cronjobs

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.

How do I configure cronjobs at Hipex?

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)

View, add and remove cronjobs

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.

Adding 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.

crontab -e

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/

Variables

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:

echo $PATH

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/

Viewing Cronjobs

To view cronjobs we use the โ€˜listโ€™ command. Using the command below enables you to view the actual configured rules in crontab:

crontab -l

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/

Disable or delete Cronjobs

Sometimes cronjobs need to be disabled. This is an easy task using the โ€˜editโ€™ option:

crontab -e

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.