There are times when you might need to automate the execution of some particular tasks on your web server. Some of these tasks could be maintenance tasks, backups, monitoring, etc. Just as with Microsoft Windows, Linux / Unix workstations and servers have the possibility to run shell commands periodically on a given schedule.
The service in charge of executing these commands is called CRON. The configuration containing the list of called a CRONTAB.
If you have a shared hosting account, you can skip to How To Manage Cron Jobs With cPanel.
If you have a root access to your server, you can manage the crontab from command line. The crontab file is usually located in /etc/crontab. Let’s look at the structure of the crontab file:
* * * * * shell command
A crontab may contain multiple lines like the above. Each of the first five fields may be either an asterisk (*) or a numeric value. From left to right, here are what these fields represent:
An asterisk means every [ ... ] like every minute, every hour, every day of the month, etc. Here are a few examples to help you understand better:
Scheduling A Task To Execute Once A Year
This would execute a script named backup.sh on December 1st at midnight:
0 0 1 12 * /usr/local/backup.sh
Scheduling A Task To Run Once Every 24 Hours
To set a daily task to run at 1am, you could use:
0 1 * * * /usr/local/maintenance.sh
Scheduling A Monthly Task
To run a script on the fifth of every month at 11pm, you could use:
0 23 5 * * /usr/local/back.sh
Scheduling A Weekly Task
To setup a weekly task to run each Saturday at 6:30 am:
30 6 * * 6 /usr/local/weekly-backup.sh
But there is an easier way to manage cron jobs. In the /etc/ folder you will find four cron-related subfolders:
So for example, if you wanted to run a shell script on a daily basis, you could just create it in /etc/cron.daily and set its permission to 0755 (chmod 0755 filename). If you don’t find these folders in /etc, simply issue this command:
# whereis cron
And to find out when the scripts from these folders are scheduled to be executed, refer to the /etc/crontab file.
If you have cPanel installed, it may be easier and preferable to manage the cron jobs from your control panel. This way, an account’s scheduled tasks will also be backed up with the rest of the data.
To manage the scheduled tasks from within cPanel, click on the Cron Jobs icon.
You will be then be offered to use the standard or advanced interface. From the standard interface, you could setup a task to rub backup.sh every day at 3AM:

And this is how you would do the same thing from the advanced interface:

There are a few things you want to look out for when you schedule an automated task:
And if you’re unsure about what you’re doing, it always best to get support from your hosting provider!
0 responses so far ↓
There are no comments yet...Kick things off by filling out the form below.
Leave a Comment