Early in my Linux journey, I came to appreciate the numerous command-line utilities of the operating system and the way they streamlined regular tasks. For example, backing up applications on my Windows server frequently required expensive add-on software packages. By contrast, the tar
command makes backing up Linux relatively easy, and it's powerful and reliable too.
When backing up our school district email system, however, I faced a different challenge. Backups couldn't occur during the workday or early evening because people were using the system. The backup had to occur after midnight, and it needed to be reliable. I was used to Windows Task Manager, but what was I going to use on Linux? That's when I learned about cron.
Scheduling tasks on Linux with cron
Cron is a daemon used to execute scheduled commands automatically. Learning how to use cron required some reading and experimenting, but soon I was using cron to shut down our email server, back up the data in a compressed tar file, then restart the email service at 3AM.
The commands for a cron job are stored in the crontab file on a Linux system, which is usually found in /etc/crontab. Display the contents of your crontab file with $ crontab -l
.
Edit the crontab file with $ crontab -e
.
Some systems default to the Vi editor for cron editing. You can override this setting using environment variables:
$ EDITOR=nano crontab -e
This allows you to use the nano editor to edit your personal crontab (if you don't have one yet, one is created automatically for you).
All crontab commands have parameters denoted by an asterisk until you insert an integer value. The first represents minutes, then hours, day of the month, month of the year, and finally, day of the week.
Comments are preceded by a hash. Cron ignores comments, so they're a great way to leave yourself notes about what a command does and why it's important.
A sample cron job
Suppose you want to scan your home directory for viruses and malware with clamscan every week on Monday at 10AM. You also want to back up your home directory every week on Tuesday at 9AM. Using cron and crontab files ensures that your system maintenance occurs every week whether you remember to run those utilities or not.
Edit your crontab file to include the following, using your own username instead of "don" (my user name):
# Scan my home directory for viruses
0 10 * * 1 clamscan -ir /home/don
# Backup my home directory
0 9 * * 2 tar -zcf /var/backups/home.tgz /home/don
If you're using the nano editor, save your work with Ctrl+O to write the file out and Ctrl+X to exit the editor. After editing the file, use crontab -l
to list the contents of your cron file to ensure that it has been properly saved.
You can create crontab jobs for any job required on your system. This takes full advantage of the cron daemon.
Scheduling from the Linux command line
It's no secret that the hardest part of cron is coming up with the right values for those leading asterisks. There are websites, like crontab.guru, that dynamically translate cron time into human-readable translations, and Opensource.com has a cron cheat sheet you can download to help you keep it straight.
Additionally, most modern cron systems feature shortcuts to common values, including:
@hourly
: Run once an hour (0 * * * *)@daily
: Run once a day (0 0 * * *)@weekly
: Run once a week (0 0 * * 0)@monthly
: Run once a month (0 0 1 * *)@reboot
: Run once after reboot
There are also alternatives to cron, including anacron for jobs you want to run regularly but not according to a specific schedule, and the at
command for one-off jobs.
Cron is a useful task scheduling system, and it's as easy to use as editing text. Give it a try!
2 Comments