Linux Basics: Scheduling Tasks Using Cron

April 17th, 2008 · No Comments

Linux Cron TutorialThere 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.

How To Manage Scheduled Tasks On A Dedicated Server

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:

  1. minute (0 - 59)
  2. hour (0 - 23)
  3. day of the month (1 - 31)
  4. month (1 - 12)
  5. day of the week (0 - 6, where Sunday = 0)

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:

  • /etc/cron.hourly
  • /etc/cron.daily
  • /etc/cron.weekly
  • /etc/cron.monthly

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.

How To Manage Cron Jobs With cPanel


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.

cPanel Cron Jobs

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:

cPanel Cron Job

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

cPanel Cron Jobs

Things To Be Careful About

There are a few things you want to look out for when you schedule an automated task:

  • Make sure it is not too resource-consuming or it could affect your web server’s performance (or have your shared hosting account suspended).
  • Make sure that a script’s execution time is shorter than its execution cycle.
  • Make sure to include the full path to every binary and other files you use in your script
  • If you’re using the crontab folders, don’t forget to set the right permissions (0755) on your scripts.
  • You may want to redirect the output to either a log file (be careful not to let grow too large) or to nowhere (>> /dev/null).

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




Posted in Linux · Tutorials | No Comments

Dedicated Servers
 
VPS
Website Hosting
 

Recent Comments

Recent Webmasters

Hosting Type :
Monthly Price :
Storage :
Transfer :
Sort By :
Search