The Complete Beginners Guide to Cron, Part 1
Cron is one of the most popular and basic utilities found on Unix systems. Combined with other tools, cron makes it exceptionally easy to automate a broad range of tasks on your server.
The purpose of this tutorial is to give a brief intro to some of the neat things you can do with cron. In part 2, we’ll bring it all together to make a very simple script that will email basic information about your server to you.
While anyone may find aspects of this guide useful, it’s primarily aimed at beginners that are ready to explore some of the tools available for basic server administration.
By the end of this tutorial you should be able to:
- Create scheduled backups (or anything else)
- Write basic scripts and schedule them
We’ll be using a few different tools:
- You’ll need root access to your server
- Cron – A job scheduler that’s included in nearly all unix based operating systems.
- Basic Linux CLI administration
- A very small amount of Shell programming
If you’re a beginner and the above sounds a little daunting, don’t worry. This is more straight-forward and simpler than you’re imagining.
Cron – Let’s get started
Cron allows for very easy scheduling (automation) of tasks and is most often deployed by systems administrators on servers. Here are some common tasks that are scheduled with cron:
- Creating backups,
- Checking disk space,
- Running a maintenance script,
- Periodically deleting files,
- And pretty much anything you can imagine.
To begin, connect to your server via SSH and open your crontab. The crontab is specific to each user. To list the current contents of your crontab:
To open your current user’s crontab for editing:
To open the crontab of a different user:
crontab -u user2 -e
When you open your crontab, depending on your Linux distribution, you may see nothing or an output similar to the following:
Edit this file to introduce tasks to be run by cron.
Each task to run has to be defined through a single line
indicating with different fields when the task will be run
and what command to run for the task….
The crontab file allows you to easily schedule one or more tasks, referred to as ‘jobs’, based on specific timeframes. Keep in mind that this file is user specific and a job created by one user will not be visible to another. This is important to consider when working on a server with other users. Concurrently scheduling several jobs could potentially tie up large amounts of system resources.
In order to add tasks to your crontab, scroll to the bottom if necessary. You may see a line defining the syntax:
m h dom mon dow command
The scheduling works like this:
* * * * command
The asterisks (*) correspond to specific blocks of time:
Minute (0-59) Hour (0-24) Day (1-7) Month (1-12) Weekday (0-6) command
You use numbers in place of asterisks to dictate when the specified command will run.
Schedule Automatic Backups
To help you get the hang of the syntax, let’s combine the ‘tar’ command with cron to create scheduled backups. This is one of the most common ways that cron is used and it is very easy to configure.
Important: Make sure that your user has access to the folder that you are backing up.
We can create simple backups of files or directories like this:
tar -zcvf backup1.tar.gz path/to/files/
To create dated backups, you could use the following format:
tar -zcvf “$(date ‘+%y-%m-%d’).tar.gz” path/to/files/
Note: If you choose to use the date format in your crons, you’ll have to ‘escape’ () the % characters. This is because cron treats the % sign as a new line character. This would look like this:
tar -zcvf “$(date ‘+%y-%m-%d’).tar.gz” path/to/files/
It’s more useful to have backups created automatically. Let’s combine the above command with cron to backup a specific directory every Monday at 11:15pm. We’ll open our crontab and add our schedule and backup command:
15 23 * * 1 tar -zcvf backup1.tar.gz path/to/files/
That’s the 15th minute of the 23rd hour of the first day (Monday) of each week. Or perhaps you need it to run each evening at 8:30pm:
30 20 * * * tar -zcvf backup1.tar.gz path/to/files/
That’s the 30th minute of the 20th hour of each day.
You can also use slashes (/) to assign specific time intervals. The cron below will run every 10 hours.
0 */10 * * * tar -zcvf backup1.tar.gz path/to/files/
To help you get the hang of it, there’s a tool located here that breaks the syntax down nicely.
If you’re familiar with rsync, you can easily schedule your backups to be periodically moved to a remote location:
* * * * * rsync -avz path/to/backup1 email@example.com:/path/to/backups/
For help with rsync (a great tool to know about), check out this recent blog post, The Many Uses of Rsync.
You can schedule tasks to run at specific intervals using simple shortcuts in place of the asterisks or numbers. You can also use this to assign tasks to occur on system startup:
|@reboot||Run once, at startup.|
|@yearly||Run once a year, “0 0 1 1 *”.|
|@annually||(same as @yearly)|
|@monthly||Run once a month, “0 0 1 * *”.|
|@weekly||Run once a week, “0 0 * * 0”.|
|@daily||Run once a day, “0 0 * * *”.|
|@midnight||(same as @daily)|
|@hourly||Run once an hour, “0 * * * *”.|
A cron in that format would look like this:
@weekly tar -zcvf backup1.tar.gz path/to/files/
Stay tuned for our next installment, where we set the email function and learn how to send basic system status updates!