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

Whats Next for AI: Interpreting Anthropic CEOs Vision

Friday, November 22, 2024

Top Tech Content sent at Noon! How the world collects web data Read this email in your browser How are you, @newsletterest1? 🪐 What's happening in tech today, November 22, 2024? The HackerNoon

iOS Cocoa Treats

Friday, November 22, 2024

View in browser Hello, you're reading Infinum iOS Cocoa Treats, bringing you the latest iOS related news straight to your inbox every week. Using the SwiftUI ImageRenderer The SwiftUI ImageRenderer

iOS Dev Weekly - Issue 688

Friday, November 22, 2024

How do you get an app featured on the App Store? There's a new process, and it's great! 📝 View on the Web Archives ISSUE 688 November 22nd 2024 Comment Every developer, from solo indie devs to

Why Nvidia's CEO loves NotebookLM

Friday, November 22, 2024

I love my Alexa-enabled microwave; Best early Black Friday deals -- ZDNET ZDNET Tech Today - US November 22, 2024 Jensen Huang Even Nvidia's CEO is obsessed with Google's NotebookLM AI tool

Digest #151: Uber’s Migration, Terraform Tips, AMI Creation, and Helm Chart Scanning

Friday, November 22, 2024

Learn zero-downtime migration techniques, improve testing workflows, and master AMI creation. Plus, explore Terraform tools, Helm chart validation, and debugging AWS EC2 issues. ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏

SWLW #626: AI makes Tech Debt more expensive, The problem with most L&D strategies, and more.

Friday, November 22, 2024

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

Warning: Over 2,000 Palo Alto Networks Devices Hacked in Ongoing Attack Campaign

Friday, November 22, 2024

THN Daily Updates Newsletter cover Generative AI For Dummies ($18.00 Value) FREE for a Limited Time Generate a personal assistant with generative AI Download Now Sponsored LATEST NEWS Nov 22, 2024

⚙️ Businesses increase AI spend to $13.8 billion

Friday, November 22, 2024

Plus: World leaders endorse digital green action plan ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌

Post from Syncfusion Blogs on 11/22/2024

Friday, November 22, 2024

New blogs from Syncfusion Building Oqtane Modules with Syncfusion Components for Blazor [Webinar Show Notes] By Carter Harris This blog provides show notes for our Nov. 14, 2024, webinar, “Building

ASP.NET Core News - 11/22/2024

Friday, November 22, 2024

View this email in your browser Get ready for this weeks best blog posts about ASP.NET Core! Integrating .NET Aspire With Azure Storage — by fsazanavets Lanayx/Oxpecker: ASP.NET Core based F# framework