VOOZH about

URL: https://thenewstack.io/linux-how-to-use-cron-to-schedule-jobs/

⇱ Linux: How to Use Cron to Schedule Regular Jobs - The New Stack


TNS
SUBSCRIBE
Join our community of software engineering leaders and aspirational developers. Always stay in-the-know by getting the most important news and exclusive content delivered fresh to your inbox to learn more about at-scale software development.
REQUIRED
It seems that you've previously unsubscribed from our newsletter in the past. Click the button below to open the re-subscribe form in a new tab. When you're done, simply close that tab and continue with this form to complete your subscription.
The New Stack does not sell your information or share it with unaffiliated third parties. By continuing, you agree to our Terms of Use and Privacy Policy.
Welcome and thank you for joining The New Stack community!
Please answer a few simple questions to help us deliver the news and resources you are interested in.
REQUIRED
REQUIRED
REQUIRED
REQUIRED
REQUIRED
Great to meet you!
Tell us a bit about your job so we can cover the topics you find most relevant.
REQUIRED
REQUIRED
REQUIRED
REQUIRED
REQUIRED
Welcome!

We’re so glad you’re here. You can expect all the best TNS content to arrive Monday through Friday to keep you on top of the news and at the top of your game.

What’s next?

Check your inbox for a confirmation email where you can adjust your preferences and even join additional groups.

Follow TNS on your favorite social media networks.

Become a TNS follower on LinkedIn.

Check out the latest featured and trending stories while you wait for your first TNS newsletter.

PREV
1 of 2
NEXT
VOXPOP
As a JavaScript developer, what non-React tools do you use most often?
Angular
0%
Astro
0%
Svelte
0%
Vue.js
0%
Other
0%
I only use React
0%
I don't use JavaScript
0%
Thanks for your opinion! Subscribe below to get the final results, published exclusively in our TNS Update newsletter:
NEW! Try Stackie AI
From clobbered drafts to real-time sync
Apr 14th 2026 10:00am, by David Moore
TypeScript 6.0 RC arrives as a bridge to a faster future
Mar 14th 2026 9:00am, by Darryl K. Taft
Mastra empowers web devs to build AI agents in TypeScript
Jan 28th 2026 11:00am, by Loraine Lawson
2025-01-05 06:00:52
Linux: How to Use Cron to Schedule Regular Jobs
tutorial,
Linux / Operations

Linux: How to Use Cron to Schedule Regular Jobs

With Cron, you can schedule just about any type of job on Linux, such as backing up specific directories to an external drive.
Jan 5th, 2025 6:00am by Jack Wallen
👁 Featued image for: Linux: How to Use Cron to Schedule Regular Jobs
Feature image via Pixabay.

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:

crontab -l

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:

0 23 * * 0

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:

59 23 * * 6

Or you could state it as:

59 23 * * Saturday

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:

sudo mkdir /backup

Change the permissions so your user can write to it with:

sudo chmod -R u+w /data

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 chmod u+x /usr/local/bin/sat_backup.sh

Before you continue, make sure the script runs as expected by issuing the command:

sat_backup.sh

Now, let’s create the cronjob. For that, issue the command:

crontab -e

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:

0 23 * * 0 /usr/local/bin/sat_backup.sh > /dev/null 2>&1

A bit of explanation is necessary.

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:

crontab -l

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.

TRENDING STORIES
Jack Wallen is what happens when a Gen Xer mind-melds with present-day snark. Jack is a seeker of truth and a writer of words with a quantum mechanical pencil and a disjointed beat of sound and soul. Although he resides...
Read more from Jack Wallen
SHARE THIS STORY
TRENDING STORIES
SHARE THIS STORY
TRENDING STORIES
TNS DAILY NEWSLETTER Receive a free roundup of the most recent TNS articles in your inbox each day.
The New Stack does not sell your information or share it with unaffiliated third parties. By continuing, you agree to our Terms of Use and Privacy Policy.