The Linux/Unix cron is a command line, table-based Linux scheduling program that runs jobs at specific times. System administrators use it to schedule maintenance jobs like backups, downloads, disk cleanups and other such periodic repetitive activities. Most Debian-based Linux versions allow normal users to run their own cron jobs by default. The root user can always use cron.
We talked about archiving files in tar recently. Using it with cron is a straightforward way to do file backups on a regular basis. cron can also be used in testing, development and everyday automation of lots of your computing tasks. I occasionally use it with MQTT to generate a “heartbeat” and make sure a remote internet of things (IoT) system is still up and running.
Let’s see how you can use cron to automate Linux tasks.
Using cron isn’t difficult. You set up your program schedule in a cron table and write it out to disk. The cron process then picks it up and runs your program at the specified. Scheduling can be from minutes to days to years with lots of combinations. The easiest way to edit the table is at the command line with the crontab command using the “-e” option.
rob% crontab -e
This command-line brought up the nano text editor on my ASUS notebook under Xubuntu. I like to use vi so I just added “export EDITOR=vi” to the rob user .bashrc file. A quick logout/login made the change take effect and poof, my default text editor became vi. Interestingly, vi is the default text editor under Raspbian on my Raspberry Pi 4 steampunk notebook computer. cron and tar work great there too.
Writing and quitting vi installs the new cron table and updates the list for execution automatically. You’ll get a message saying something like “installing new crontab” indicating that everything is correct. Problems in the file, like forgetting a space or an ill-form command line won’t install the table and your command won’t run as you expected. I chose the following example to give the reader some practice before committing to doing an automatic archive with tar. Better to make sure the basics work before waiting a few hours or days to see if your first archive actually ran.
Here’s an example of writing a message to a file at a certain time.
In this example the last line is the only actionable item in the file. All the previous lines are comments and instructions. cron tables have a specific format of minutes, hours, day of the month, month, day of the week and the command you want to execute, each separated with a space. Comment lines are denoted with a pound sign (“#”) as the first character. Each line is evaluated separately and run at the designated time.
The actionable line sequentially writes the word “hello” out to a file named robcron.txt using the echo command.
46 * * * * echo "hello" >> robcron.txt
The echo command will run at 46 minutes after every hour. The double right arrows tell it to add “hello” to the robcron.txt each time it runs. The “*” symbol for the other hours, day of week, etc. parameters run on all instances of those fields. Hours are 0 to 23, days are 1 to 31 and so on.
You can use a “,” for multiple times in a particular field. Days might be 3,10 indicating that the echo command would run on the third and tenth day of the month. Likewise, a “-” indicates a range of values for the field. One to three might be an appropriate value for the day of week, meaning run echo on Monday, Tuesday and Wednesday.
Using cron on portable Linux systems, such as laptops can be problematic since the commands won’t get executed if the machine happens to be switched off at the designated time. There’s no provision to run a job when the power is turned back on. Of course, that shouldn’t discourage you from using cron. Just be aware of the limitation and plan accordingly. I generally run my Linux notebook 24/7 when I’m at home.
Now let’s see how we can automatically make backups with tar using cron.
Automatic tar Backups
Say I want to create a backup file of one of my important directories with tar every afternoon at 1 p.m. important-files contains the target files under my home directory.
I could edit the existing example in my cron table to reflect the new function. Just fire up old crontab -e and make the last line look like the following.
0 13 * * * tar -cvf robcron.tar important-files
In this example, I had a couple of LibreOffice Impress conference slide stacks and a graphic file in the important-files directory.
Since I was working on this tutorial after Noon, I exited crontab and waited until 1 p.m. At 1:01 p.m. I checked for robcron files with ls.
rob% ls -l robcron*
Sure enough robcron.tar appeared in the listing.
It always makes sense to assure yourself that everything works as expected.
To verify that the tar file was created correctly, I copied it into a “test” directory, then expanded the archive into their individual files.
rob% makedir test
rob% cp robcron.tar test
rob% cd test
rob% tar -xvf robcron.tar
Listing the files in test showed the following.
I then brought up one of the slide stacks in LibreOffice to make sure the file was good. It worked fine.
Cron has a bunch of different options that readers can explore starting with the crontab page. The web has a variety of articles and other tutorials, as well.
Another interesting idea is to investigate using scripts with cron. Write your command line script and call it just like you call tar inside the cron table. For example, in addition to making a tar file of important files to back up, why not ship the resultant file to a network server using FTP or a similar program. Maybe make a listing of the files, for searching/retrieving purposes. Those jobs could be bundled up in a simple script. Oh yea, don’t forget to make your scripts executable.
The Linux Foundation is a sponsor of The New Stack.