When you use Linux, you have everything at your disposal to create a powerful, flexible, and automated environment for whatever purpose you need.
One of the tools available for automation is called Cron. With Cron, you can schedule just about any type of job on Linux. For example, you might write a bash script to back up specific directories to an external drive. If you want to have that backup script run automatically, you’ll want to create what’s called a cronjob, so it’ll run exactly when you want.
Cron is installed on nearly every Linux distribution by default, so there’s nothing you need to add to automate jobs.
But how is Cron used? I’m here to show you.
How Does Cron Work?
Cron depends on crontab files, which are saved in /var/spool/cron. It’s very important to understand, however, that you do not edit those files manually. Why? Because Cron has a tool you can use to edit your crontab files.
Which brings up another point. Every user on a Linux system has their own crontab files. Any user can create crontab files and even view their current cron jobs with the command:
The output of that command will display the contents of your crontab file that will contain any custom cron jobs you’ve created.
The one thing that trips most people up about Cron is the timing format. With Cron, 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)
Day of Week (0-6, although you can use Sunday, Monday, Tuesday, etc., and Sunday can be represented by 0, 7, or Sunday)
The time/date format is configured like so:
M H D MO DW
Where M is Minutes, H is hour, Day is day of the Month, MO is Month, and DW is Day of the Week.
For example, if you’ve created a backup script and you want it to run every Sunday at 11:00 p.m., the time/date format would be:
Any time Cron sees an asterisk, it knows you want it to run during every instance of that time/date entry. In the example above, the Day of the Month and Month entries have asterisks, which means they will run every day of the month and every month.
If you wanted that job to run every Saturday at 11:59 p.m., the entry would be:
Or you could state it as:
Adding a Cronjob
Let’s create a backup script and then create a cron job so it will run every Sunday at 11 p.m.
Our backup script looks 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
Save that file as sat_backup.sh. You’ll also want to create the /backup directory with:
Change the permissions so your user can write to it with:
You’ll also want to make sure the directory ownership is correct with something like:
sudo chown–R$USER:$USER/data
Once you’ve saved the backup script, move it to /usr/local/bin with:
sudo mv sat_backup.sh/usr/local/bin
Give the script executable permissions with:
sudo chmodu+x/usr/local/bin/sat_backup.sh
Before you continue, make sure the script runs as expected by issuing the command:
Now, let’s create the cronjob. For that, issue the command:
If this is the first time you’ve run the above command, you’ll be prompted to select your default editor. I would highly recommend selecting Nano, as it’s very easy to use.
At the bottom of the file, you’ll add the following:
You already know what 0 23 * * 0 is and /usr/local/bin/sat_backup.sh is the script you created. But what about /dev/null 2>&1? That’s a key element because if the script produces any output (even errors) it will fail without knowing where to send the errors. In the case above, /dev/null 2>&1 is essentially a trash can. Without that piece of the puzzle, if any errors were found, the cron job would fail to run. To get even more specific:
2 is a file descriptor for standard errors
> is used for the redirect
& is the symbol for file descriptor
1 is the file descriptor for standard output
This means all standard errors are redirected to standard output and are sent to the black hole known as /dev/null.
Save the cronjob with the Ctrl+x keyboard shortcut. You can then view your cronjob with:
You should see the new cronjob listed, and next Sunday at 11 p.m., the job will automatically run, and you’ll have a new backup file found in /data.
And that’s all there is to Linux cronjobs. This tool will certainly come in handy for you as a Linux user and/or administrator.
YOUTUBE.COM/THENEWSTACK
Tech moves fast, don’t miss an episode. Subscribe to our YouTube
channel to stream all our podcasts, interviews, demos, and more.
LinkedIn report finds AI and sustainability are some of the most sought-after rolesWorkers will need brand-new skills for many roles by 2030Upskilling efforts
As people refresh, reset and reprioritize in the new year, some may be looking to make some big changes at work.A growing number of American workers report feel
LAKE CHARLES, La. (KPLC) - With holiday and seasonal jobs now gone, people are looking for new work opportunities.Crystal Scott works for the American Job Cente
DOVER, Del., Jan. 6, 2025 /PRNewswire/ -- CareerEco, a leading virtual event platform specializing in connecting organizations with talent, is thrilled to a