Often, you’ll want to use cron to schedule daily, weekly, hourly (etc…) tasks on your linux system(s). cPanel has a little GUI for it, but since I don’t trust GUIs very often, I like just doing it the ‘old fashioned’ way. This little guide will hopefully help you to figure out cron once and for all.
Ok, so cron is what is used on your Linux system to schedule repeating events. It’s used to rotate logs, run clean-up scripts, and anything else you can think of that you’d want to schedule.
To view your crontab (view the current cron jobs for your user) simply type in:
1 |
crontab -l |
To edit your crontab (edit the current cron jobs for your user) type in:
1 |
crontab -e |
If you are root and you want to edit another user’s crontab, you can specify them like:
1 |
crontab -u larry -e |
A typical cron entry looks like this:
1 |
10 22 * * * /usr/local/bin/mysql_backup.sh |
Let’s break this down. There are 5 fields before the actual command:
From the man page:
1 2 3 4 5 6 7 |
field allowed values ----- -------------- minute 0-59 hour 0-23 day of month 1-31 month 1-12 (or names) day of week 0-7 (0 or 7 is Sun, or use names) |
For ‘month’ or ‘day of week’, you can use the first 3 letters – case doesn’t matter.. ie: sun, mon, tues… or jan, feb, mar.. etc..
Then, you’ll see the command – in this case, it’s a mysql backup script.
Let’s run through a couple examples:
Run a command once/week scheduled Saturday morning at 6am:
1 |
0 6 * * sat /path/to/command |
or
1 |
0 6 * * 6 /path/to/command |
Now, let’s only run it on Saturdays in August:
1 |
0 6 * aug sat /path/to/command |
How about every 5 minutes all the time:
1 |
*/5 * * * * /path/to/command |
How about every other day at 10am!
1 |
00 10 */2 * * /path/to/command |
Hopefully, this will help clear up any confusion you’ve had with cron! Let us know below!
For some of the users, they don’t have cron on server, or the server limits the cron use. In those cases, EasyCron is an option. To those who don’t know how to use crontab, EasyCron is a great alternative 🙂