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

Vlt client & registry; ESMeta

Tuesday, November 5, 2024

We have 2 links for you - Stay up-to-date on JavaScript and tools Two vlt products: a better npm client and a serverless package registry www.vlt.sh @vltpkg@fosstodon.org vlt has launched two products:

Want a programming job? Learn this

Tuesday, November 5, 2024

Stop background Android apps; AI election tracker; Early Black Friday sales -- ZDNET ZDNET Tech Today - US November 5, 2024 Python code Your dream programming job demands this language, every site

⚙️ The battle for AI regulation

Tuesday, November 5, 2024

Plus: Perplexity's AI hub ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌

Post from Syncfusion Blogs on 11/05/2024

Tuesday, November 5, 2024

New blogs from Syncfusion 6 Effective Ways to Merge PDF Files Using C# By Chinnu Muniyappan This blog explains the six effective methods to merge PDF files using C# and the Syncfusion .NET PDF Library

Google Warns of Actively Exploited CVE-2024-43093 Vulnerability in Android System

Tuesday, November 5, 2024

THN Daily Updates Newsletter cover The Data Science Workshop: Learn how you can build machine learning models and create your own real-world data science projects, Second Edition ($35.99 Value) FREE

Issue 161

Tuesday, November 5, 2024

🤖👮 Atlanta prison introduces 6-foot tall AI-powered robot guards. Meta's blood money: how Facebook turns tragedy into profit. OpenAI safety expert quits: "We're not ready for what's

Edge 445: A New Series About Knowledge Distillation

Tuesday, November 5, 2024

In this issue: ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏ ͏

New Blogs on ThomasMaurer.ch for 11/05/2024

Tuesday, November 5, 2024

View this email in your browser Thomas Maurer Cloud & Datacenter Update This is the update for blog posts on ThomasMaurer.ch. Honored to Receive the YouTube Silver Creator Award By Thomas Maurer on

📱 I Tried Running Ubuntu on My Phone — Samsung's One UI Is How Android Should Be

Monday, November 4, 2024

Also: The Most Realistic Game Simulations, and More! How-To Geek Logo November 4, 2024 Did You Know Peter Weller, best known for his role as Robocop, is an accomplished academic and actor. He has a

Ranked | America’s Most Popular Drugs by Dollars Spent 💰

Monday, November 4, 2024

Tired of hearing about Ozempic? This chart isn't for you. It's one of America's most popular drugs in 2023. Here are some numbers. View Online | Subscribe | Download Our App Presented by: