A cron job is a Linux command that runs at regular intervals. These “jobs” can be scheduled through the command line, but it is much easier to do so through the cPanel GUI. cPanel also conveniently shows the number of existing cron jobs and can also send command output via email. In this article, we will show you how to schedule cron jobs easily and efficiently.
Step 1: Go to the cron job page in cPanel
The first step is to log into cPanel and scroll down until you find the section labeled “Advanced” and then select “Cron Jobs” as shown here.
Now let’s create a job.
Step 2. Select Email Settings and Schedule
You have an option in cPanel to send email whenever this cron job runs. This can be useful if you need to keep track of specific commands that you are using. In the next section, enter your email address if you like:
From the dropdown list, you can quickly select some general schedules for your work. In most cases, you can simply select one of the pre-selected options. Here, for example, we run the command once a week with the default settings:
If you want something more complex, please explain what the fields mean.
As indicated in the text boxes, you can enter the following values:
- Minute – 00 to 59
- Hour – 0 to 23
- Day 1 – 31
- Month – 1 to 12
- Day of the week – 0 to 6
These five values will allow you to create any complex schedule you want. Each of these fields takes on a value that applies to it. So to take “weekday” as an example, “0” means Sunday, “1” means Monday, and so on.
Using an asterisk or an asterisk
in a field means it doesn’t matter what the value is. The work is performed when the date and time on the server match all the specified values. Any meaning with a star
0 0 * * 0
will match by default.
In the example above, we wanted the task to run once a week. So the only thing we needed to specify was a weekday – in this case 0. We also want to make sure that the job is done only once on Sunday and not every minute, so we specify the minute and hour. Placed, “once a week” can be written as:
It is so simple!
mysqlcheck --all-databases --optimize --verbose
After you get the schedule you want, enter the Linux command you want to run in the box labeled “Command”. In this example, we select the following:
How to set up cron to work in cPanel
This command will optimize all databases on all websites hosted on the server.
Suppressing exit and error messages
Many Linux commands dump output to the terminal. Sometimes these messages can be very long. Choosing to receive email notifications can get extremely annoying.
You can suppress normal, regular Linux command output by adding the following after it:
mysqlcheck --all-databases --optimize --verbose >/dev/null
So the command above would be:
However, this will not suppress error messages. And this is something useful. While you can ignore the regular output, you probably want to be notified if something went wrong. However, we can also suppress error messages by adding the following to the end of the commands:
Additional “2> & 1” redirects error messages to standard output, which we automatically ignore with the previous “> / dev / null”.
This way, you can pick and choose exactly what you want to see when your command runs.
Now just click on “Add New Cron Job” after entering the command and you’re done! You have successfully set up your Linux cron job!