'ZDNET Recommends': What exactly does it mean?
ZDNET's recommendations are based on many hours of testing, research, and comparison shopping. We gather data from the best available sources, including vendor and retailer listings as well as other relevant and independent reviews sites. And we pore over customer reviews to find out what matters to real people who already own and use the products and services we’re assessing.
When you click through from our site to a retailer and buy a product or service, we may earn affiliate commissions. This helps support our work, but does not affect what we cover or how, and it does not affect the price you pay. Neither ZDNET nor the author are compensated for these independent reviews. Indeed, we follow strict guidelines that ensure our editorial content is never influenced by advertisers.
ZDNET's editorial team writes on behalf of you, our reader. Our goal is to deliver the most accurate information and the most knowledgeable advice possible in order to help you make smarter buying decisions on tech gear and a wide array of products and services. Our editors thoroughly review and fact-check every article to ensure that our content meets the highest standards. If we have made an error or published misleading information, we will correct or clarify the article. If you see inaccuracies in our content, please report the mistake via this form.
Linux is one of the most flexible operating systems on the planet. There is very little you can't do with Linux… even automate tasks using a simple command line tool.
The tool in question is called cron and it allows you to schedule jobs for the Linux operating system.
Also: Patch now: Serious Linux security hole uncovered
Say, for example, you've written a simple backup script to back up everything in your Documents folder. It's named backup.sh and looks something like this:
#!/bin/bash # What you want to backup. backup_files="/home/$USER/Documents" # Where you want to backup to. dest="/backup" # Create an archive filename. day=$(date +%A) hostname=$(hostname -s) archive_file="$hostname-$day.tgz" # Backup the files using tar. tar czf $dest/$archive_file $backup_files
You save that script in /usr/local/bin and give it the proper execution permissions with the command:
sudo chmod u+x /usr/local/bin/backup.sh
Now, instead of running that backup script manually every day or week, you can use cron to make it automatic. Let me show you how.
If you don't already have your terminal window open, do so now.
The cron system has its own build-in editor for cronjobs. To open your crontab in edit mode, issue the command:
If this is the first time you've issued the crontab -e command, you'll need to select your default editor. I would suggest going with nano, as that's the easiest Linux text editor to use.
At the bottom of the file, you'll create the new cron job entry. This is where it gets a bit tricky. You see, the time/date you use comes in a very specific form. There are five entries for time and date, which are minutes (0-59), hours (0-23), day of the month (1-31), month (1-12), and day of week (0-6, although you can use Sunday, Monday, Tuesday, etc., and Sunday can be represented by 0, 7, or Sunday). Let's say you want to run your backup every Sunday at 11 pm, the crontab time/date entry would be 0 23 * * 0. If you wanted that cron job to start at 11:59 pm every Friday, the entry would be 59 23 * * 5.
The complete cron entry for a Saturday 11:59 PM run would look like this:
59 23 * * 5 /usr/local/bin/backup.sh > /dev/null 2>&1
What is the > /dev/null 2>&1 portion of the entry? Simply put, if there's any output from the script, it must be suppressed; otherwise it could cause errors. For that, we use the > to send all output to /dev/null (which a like a system trash can) and then instructs cron where to send all errors with 2>&1.
Save and close the file with Ctrl+X. Once you've saved the crontab file, the job is ready and will be run at the configured time. Before the first run of the job, you might want to test the script to make sure it completes without errors, which can be done with the command backup.sh.
And that's what cron does for you and how you can easily use it to automate scripts you've written for the Linux operating system.