Early access: Get the most from cron in Linux

Early access: Linux cron cheat sheet
Download our new Linux cron cheat sheet
Opensource.com

Get the most from cron in Linux

Making things happen on a regular and predictable schedule is important on computers. It's important because, as humans, we can sometimes be bad at remembering to do things reliably because we get distracted, have too much on our minds, or we're on holiday. Computers are really good at doing things on a schedule, but a human has to program the computer before the computer takes action.

Early access: cron cheat sheet

Download now

In a way, the cron system is an easy and rudimentary introduction to programming. You can make your computer do what you want it to do just by editing a file. You don't even have to know where the file is kept. You have only to type in a simple command, enter the "recipe" you want your computer to follow, and save your work. From then on, your computer executes your instructions at the specified time until it is told to stop.

By design, cron is not a complex system. Here's what you need to know about it.

What is cron?

The cron command is so ubiquitous in Linux and Unix, and it's been mimicked and reinvented so often that it's almost a generic term for something that happens on a schedule. It's a form of automation, and although there are different implementations of it (Dillon's cron, Vixie's cron, chrony, and others), and variations like anacron and systemd timers, the syntax and workflow has remained essentially the same for decades.

Cron works on a "spool" system, much like printers and email. If you didn't know that printers and email use a spool, that's okay because the point of a spool file is that you aren't supposed to think about it much. On a Linux system, the directory /var/spool is designed as a central hub for important but low-level files that the user isn't meant to interact with directly. One of the spools managed in /var/spool is cron tables or "crontab" for short. Every user—yourself included—on a Linux system has a crontab. Users can edit, view, and remove their own crontab. In addition, users can use their crontab to schedule tasks. The cron system itself monitors crontabs and ensures that any job listed in a crontab is executed at its specified time.

Edit cron settings

You can edit your crontab using the crontab command along with the -e (for edit) option. By default, most systems invoke the vim text editor. If you, like me, don't use Vim, then you can set a different editor for yourself in your ~/.bashrc file. I set mine to Emacs, but you might also try Nano, Kate, or whatever your favorite editor happens to be. The EDITOR environment variable defines what text editor you use in your terminal, while the VISUAL variable defines what editor you use in a graphical mode:

export EDITOR=nano
export VISUAL=kate

Refresh your shell session with your new settings:

$ source ~/.bashrc

Now you can edit your crontab with your preferred editor:

$ crontab -e

Schedule a task

The cron system is essentially a calendaring system. You can tell cron how frequently you want a job to run by using five different attributes: minute, hour, date, month, weekday. The order of these attributes is strict and not necessarily intuitive, but you can think of them as filters or masks. By default, you might think of everything being set to always or every. This entry would run touch /tmp/hello at the top of every minute during every hour of every day all year long:

* * * * * touch /tmp/hello

You can restrict this all-encompassing schedule by setting specific definitions for each attribute. To make the job run on the half-hour mark of each hour, set the minutes to 30:

30 * * * * touch /tmp/hello

You can further constrain this instruction with a specific hour. This job runs at 3:30 AM every morning:

30 3 * * * touch /tmp/hello

You can also make the job run only on the first of each month:

30 3 1 * * touch /tmp/hello

You can set a month using 1 for January up to 12 for December, and you can set a day using 0 for Sunday up to 6 for Saturday. This job runs at 3:15 during the month of April, only on Mondays:

15 3 * 4 1 touch /tmp/hello

Set increments

All of these settings match a value exactly. You can also use cron notation to run jobs after a set passage of time. For instance, you can run a job every 15 minutes:

*/15 * * * * touch /tmp/hello

You could run a job at 10 AM every three days:

* 10 */3 * * touch /tmp/hello

You could run a job every six hours:

* */6 * * * touch /tmp/hello

Cron shorthand

Modern cron implementations have added a convenient shorthand for common schedules. These are:

  • @hourly
  • @daily
  • @weekly
  • @monthly
  • @yearly or @annually

List cron jobs

Using the crontab command, you can see a list of your scheduled cron jobs:

$ crontab -l
15 3 * 4 1 touch /tmp/hello

Remove a crontab

When you're done with a crontab, you can remove it with the -r option:

$ crontab -r -i

The -i option stands for interactive. It prompts you for confirmation before deleting the file.

What cron can do

It's one thing to know how to use cron, but it's another thing to know what to use it for. The classic use case is a good backup plan. If your computer is on for most of the day or all day and all night, then you can schedule a routine backup of an important partition. I run a backup application called rdiff-backup on my primary data partition daily at 3AM:

$ crontab -l | grep rdiff
* 3 * * * rdiff-backup /data/ /vault/

Another common use is system maintenance. On my Slackware desktop, I update my local repository catalog every Friday afternoon:

$ crontab -l | grep slack
* 14 * * 5 sudo slackpkg update

I could also run an Ansible script at 15:00 every three days to tidy up my Downloads folder:

$ crontab -l | grep ansible
* 15 */3 * * ansible-playbook /home/seth/Ansible/cleanup.yaml

A little investment in the health of your computing environment goes a long way. There are de-duplication scripts, file size and /tmp directory monitors, photo resizers, file movers, and many more menial tasks you could schedule to run in the background to help keep your system uncluttered. With cron, your computer can take care of itself in ways I only wish my physical apartment would.

Remember cron settings

Besides coming up with why you need cron, the hardest thing about cron in my experience has been remembering its syntax. Repeat this to yourself, over and over until you've committed it to memory:

Minutes, hours, date, month, weekday.

Minutes, hours, date, month, weekday.

Minutes, hours, date, month, weekday.

Better yet, go download our free cheatsheet so you have the key close at hand when you need it the most!

Did a friend forward you this email? Subscribe here to receive our weekly highlights and more.

Opensource.com aspires to publish all content under a Creative Commons license but may not be able to do so in all cases. You are responsible for ensuring that you have the necessary permission to reuse any work on this site. Red Hat and the Red Hat logo are trademarks of Red Hat, Inc., registered in the United States and other countries. Linux is a registered trademark of Linus Torvalds.

contact us | unsubscribe | about Opensource.com | redhat.com

© 2021 Opensource.com

100 E Davie Street
Raleigh, NC 27601 USA

Older messages

A simple CSS trick for dark mode, use Rust for embedded development, and more

Tuesday, November 9, 2021

5 lessons I learned about chaos engineering for Kubernetes 5 lessons I learned about chaos engineering for Kubernetes Opensource.com THE LATEST A simple CSS trick for dark mode The ability to let your

7 handy tricks for using the Linux wget command, 4 Linux tools to erase your data, and more

Tuesday, November 2, 2021

Get memory use statistics with this Linux command-line tool Get memory use statistics with this Linux command-line tool Opensource.com THE LATEST 7 handy tricks for using the Linux wget command

New downloadable guide: Chaos engineering for Kubernetes

Thursday, October 28, 2021

Get started with Kubernetes using chaos engineering Get started with Kubernetes using chaos engineering Opensource.com This downloadable eBook is a guide to getting started with Kubernetes using chaos

Create a timer on Linux, 9 ways to use open source every day, and more

Tuesday, October 26, 2021

My favorite LibreOffice productivity tips 5 open source tabletop RPGs you should try Opensource.com THE LATEST Create a timer on Linux A tutorial showing how to create a POSIX-compliant interval timer.

New Linux cheat sheet: wget command

Thursday, October 21, 2021

Download our new Linux wget command cheat sheet. Download our new Linux wget command cheat sheet. Opensource.com The wget command allows you to download files from the Internet in your terminal instead

You Might Also Like

22 CES products you can't miss

Friday, January 10, 2025

10 must-install Linux apps; Cybersecurity in 2025; Email encryption how-to -- ZDNET ZDNET Tech Today - US January 10, 2025 CES logo 2025 CES 2025: The 22 most impressive products you don't want to

⚙️ The wild, wild west

Friday, January 10, 2025

AI's uncertain legislative path ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌

ASP.NET Core News - 01/10/2025

Friday, January 10, 2025

View this email in your browser Get ready for this weeks best blog posts about ASP.NET Core! Discover the Exciting New Features in .NET Aspire 9 — by rijsat Building a Real-Time Santa's Workshop

ALERT: Ivanti Flaw CVE-2025-0282 Actively Exploited, Impacts Connect Secure and Policy Secure

Friday, January 10, 2025

THN Daily Updates Newsletter cover Deep Learning For Dummies ($21.00 Value) FREE for a Limited Time Take a deep dive into deep learning Download Now Sponsored LATEST NEWS Jan 10, 2025 Taking the Pain

The Sequence Research #466: Small but Migthy, Diving Into Microsoft Phi-4

Friday, January 10, 2025

Some architecture details about Microsoft's famous SLM. ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏

Notes app can do what now?

Friday, January 10, 2025

Hey there, Do you ever use the Notes app on your iPhone? If you do, you'll want to keep reading! The Notes app might look simple, but it has lots of great features to make your life easier. For

The Commodification of Pleasure

Friday, January 10, 2025

…and the enclosure of creative talent ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏

Invitation to the Event Sourcing workshop

Friday, January 10, 2025

Hey! I'm usually not making New Year commitments. ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏

SWLW #633: AI and cognitive offloading, The story in your head, and more.

Friday, January 10, 2025

Weekly articles & videos about people, culture and leadership: everything you need to design the org that makes the product. A weekly newsletter by Oren Ellenbogen with the best content I found

Data Science Weekly - Issue 581

Thursday, January 9, 2025

Curated news, articles and jobs related to Data Science, AI, & Machine Learning ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏