A server can be configured to perform tasks automatically, for example, running scripts or updating entire applications like WordPress or certain aspects of Magento.
For this, we have cron, which is a service that automate tasks for you and keeps track on when these tasks should be executed.
Let us first start by explaining how a cronjob is setup, the following diagram shows
how cronjobs are setup from first to last:
* * * * * command to be executed - - - - - | | | | | | | | | +----- day of week (0 - 6) (Sunday=0) | | | +------- month (1 - 12) | | +--------- day of month (1 - 31) | +----------- hour (0 - 23) +------------- min (0 - 59)
To further explain the above, let us use an example and take a look at how cPanel schedules the automatic upgrade process:
47 16 * * * /usr/local/cpanel/scripts/upcp --cron
1) The minute (47)
2) The hour (16 or 4)
3) Day of the month (* means everyday)
4) Month (* means everyday)
5) day of the week (* means everyday)
6) The actual command to be executed (/usr/local/cpanel/scripts/upcp –cron)
Now that we understand how cron tasks arranged, let us see how we can check cron tasks for the root user.
Assuming you are already logged in via SSH using root credentials, you can just type the command
crontask -u root -l
where -u means user and -l means list:
You can also execute the command to show the tasks for the user you are logged in as, in this case root:
If you need to show the cron jobs for different users, you can use the following command:
crontab -u $user -l
Replace $user with the actual account username you'd like to see, for example:
If the account does not have any cronjobs you'll see the above message, "no crontab for $user".
Please note that cron configuration files are also located "/etc" and you can see several pre-configured tasks based on day, hour, or weekly times. Common locations for these files are:
/etc/cron.d /etc/cron.daily /etc/cron.hourly /etc/cron.monthly
To view tasks configured on a daily basis we can use the following command:
ls -l /etc/cron.daily/
We will get an output similar to the following:
Each file located within these directories will contain specific commands that will be executed based on their respective times.
We need to login to cPanel for the specific account we want to configure the Cron tasks and once we have logged in we can use the search box and type in "cron".
Or we can go to:
As seen in the screenshot:
Once we have found the cron jobs interface we can go use the fields provided to create our custom cron task using the previously explained syntax.
It is worth noting you can use the "Common Settings" drop-down menu to easily setup cron jobs like "Once per minute" or "Once per 5 minutes", see screenshot.
We can also use the Cron interface to create custom tasks, for example a task that is to run with the following parameters:
At the beginning of every hour (:00) At 12:00 AM (0) On the 1st and the 15th of each Month (1,15) Every month (*) Any weekday (*)
Note: Please remember the command section of each Cron task.
Would look like this, see screenshot.
Make sure to hit "Add New CronJob" at the very end of the page, and you are set.