Skip to content

Both.org

News, Opinion, Tutorials, and Community for Linux Users and SysAdmins

Primary Menu
  • About Us
  • Computers 101
    • Hardware 101
    • Operating Systems 101
  • End of 10 Events
    • Wake Forest, NC, — 2025-09-20
  • Linux
    • Why I use Linux
    • The real reason we use Linux
  • My Linux Books
    • systemd for Linux SysAdmins
    • Using and Administering Linux – Zero to SysAdmin: 2nd Edition
    • The Linux Philosophy for SysAdmins
    • Linux for Small Business Owners
    • Errata
      • Errata for The Linux Philosophy for SysAdmins
      • Errata for Using and Administering Linux — 1st Edition
      • Errata for Using and Administering Linux — 2nd Edition
  • Open Source Resources
    • What is Open Source?
    • What is Linux?
    • What is Open Source Software?
    • The Open Source Way
  • Write for us
    • Submission and Style guide
    • Advertising statement
  • Downloads
  • Home
  • Automate Raspberry Pi tasks with crontab
  • Linux
  • Raspberry Pi
  • System Administration

Automate Raspberry Pi tasks with crontab

If you need to do a task on a regular schedule, explore how you can use crontab to do the repetitive work for you.
Jim Hall July 15, 2024 6 minutes read
checklist_todo_clock_time_team

I have a Raspberry Pi at home that I use to automate several things. For example, my printer is old enough that it doesn’t support networking, so it’s connected to the Raspberry Pi, which shares the printer to my home network using CUPS. I also use the Raspberry Pi as an at-home web server to work on websites, and as a “file server” of sorts by using SFTP from my file manager.

This became somewhat less useful last year when my Internet provider “upgraded” the home WiFi router. The new router has an added security feature where it disconnects any device from the network after about two or three days. Without a network connection, I can’t print, backup files, or work on web pages.

Restarting the Raspberry Pi would put it back on the network. So I needed to find a way to restart the Raspberry Pi every other day. And I needed it to happen automatically, and gracefully, when I wasn’t likely to use it. That’s the perfect use case for cron and crontab.

Using ‘cron’ and ‘crontab’

On Unix-like systems, cron is the system that executes tasks at specific times. The jobs that run, and when they run, are controlled using a special file called the crontab. Every user can create their own crontab to run tasks according to their own schedule.

The crontab file is arranged like a plain text “table” with fields that define the time, day, month, and day of the week to run the job. You can find detailed information about the crontab file in the crontab(1) online manual page, with this command:

$ man 5 crontab

The classic fields of the crontab file are:

FieldRange of values
1. Minute0 to 59
2. Hour0 to 23
3. Day of month1 to 31
4. Month1 to 12
5. Day of week0 to 6
6. Command to run(enter the full command here)

Modern crontab implementations, like the one on Linux, also support extensions to these fields. For example, the Vixie cron implementation (by Paul Vixie) on most Linux distributions also accepts 0 to 7 as days of the week, where 0 and 7 are the same (Sunday). You can also use names for the months and days of the week, such as sun for Sunday or jan for January. But I like using numbers, so I’ll use that here.

For example, let’s say you needed to run a tar backup of a website, every Sunday at 6:00 AM. You can create this crontab entry:

0 6 * * 0 /bin/tar czf $HOME/website.tar.gz /var/www/html

The first two fields indicate 0 for the minutes and 6 for the hour. The fifth field says 0 for the day of the week, which means “Sunday.” The third and fourth fields are * to mean “any day of the month” and “any month.” Together, these five fields will execute the tar backup every Sunday at 6:00 AM.

To enter the crontab file into the cron system, you need to use the crontab command. If your crontab file is called jobs, then you would type this:

$ crontab jobs

You can verify the contents of the crontab file with crontab -l to list (-l) the contents of the file. You can also erase your crontab from the system with crontab -r to remove (-r) the file.

Scheduled reboot

In my case, I needed to reboot the Raspberry Pi before the router took it off the network. One way to do this is with the telinit command, to change the “System V” run level. On Linux systems that run systemd, this executes the appropriate systemctl command. But I learned on original Unix systems, so I prefer telinit.

To reboot every day at 7:00 AM, I started by creating this crontab file for the root user:

0 7 * * * /sbin/telinit 6

The first two fields are 0 for the minutes and 7 for the hour, with * for all of the other fields; this will run at 7:00 AM every day. I saved this file as /root/jobs and I saved it into the system with this command as root:

# crontab /root/jobs

But I didn’t need to reboot every day; I only needed to reboot every other day. And Vixie cron has a neat syntax where you can specify / and then a number to create an alternate schedule. For example, 0-30/2 for “minutes” would run a job every other minute: “:00,” “:02,” “:04,” … and so on until “:26,” “:28,” and “:30.” This runs on even minutes because “minute” can have values from 0 to 59, and zero is an even number.

To reboot my Raspberry Pi every other day, I updated the crontab entry to use */2 for “day of the month.” This runs the job every other day: 1, 3, 5, … until 27, 29, and 31. These are odd days because “day of the month” is a value between 1 and 31, and 1 is an odd number.

0 7 */2 * * /sbin/telinit 6

With this entry, my Raspberry Pi rebooted itself at 7:00 AM every other day of the month: June 1, June 3, June 5, … and so on for the rest of the month.

Combining jobs

I figured as long as the Raspberry Pi would reboot every other day, I might as well apply updates at the same time. For that, I wanted to run the dnf command to install any updates, then use telinit to reboot the system.

When you need to combine multiple tasks into a single crontab job, I recommend collecting the commands into a script. In my case, I created this Bash script called /root/bin/update_reboot to run dnf and telinit, plus keep a log of what it did:

#!/bin/bash
# apply updates, reboot anyway

logdir=/root/log
[ -d $logdir ] || mkdir -p $logdir

today=$(date '+%d')

dnf -y update > $logdir/dnf_update.$today.log
telinit 6

In this script, I wanted to keep a private log in /root/log to capture the output from the dnf command. The two lines that set the logdir variable and run mkdir ensure that my /root/log directory is present, so I have a place to store logs.

I use the $() shell expansion to run date to print today’s day of the month as a zero-padded number, so I can use the date as part of the log file’s name when I run the dnf command to apply all updates. The last line reboots the system for me.

Automating tasks made easy

While job automation might sound like something that only system administrators do, it’s a feature that anyone can use. If you need to do a task on a regular schedule, explore how you can use crontab to do the repetitive work for you.

Tags: command line Linux SysAdmin

Post navigation

Previous: Am I a SysAdmin?
Next: Why I Use Linux

Related Stories

dark-web
  • Linux
  • News
  • Security
  • Vulnerability

High-severity Vulnerability in PackageKit

David Both April 25, 2026
OSDC_gift_giveaway_box_cropped
  • Linux
  • Package Manager

The evolution of package managers

Don Watkins April 24, 2026
water-stone-balance-eight-8
  • Fixing Windows
  • Linux

Linux to the rescue

Don Watkins April 22, 2026

Random Quote

Any sufficiently advanced technology is indistinguishable from magic.

— Arthur C. Clark

Why I’ve Never Used Windows

On February 12 I gave a presentation at the Triangle Linux Users Group (TriLUG) about why I use Linux and why I’ve never used Windows.

Here’s the link to the video: https://www.youtube.com/live/uCK_haOXPFM 

Why there’s no such thing as AI

Last October at All Things Open (ATO) I was interviewed by Jason Hibbits of We Love Open Source. It’s posted in the article “Why today’s AI isn’t intelligent (yet)“.

Technically We Write — Our Partner Site

Our partner site, Technically We Write, has published a number of articles from several contributors to Both.org. Check them out.

Technically We Write is a community of technical writers, technical editors, copyeditors, web content writers, and all other roles in technical communication.

Subscribe to Both.org

To comment on articles, you must have an account.

Send your desired user ID, first and last name, and an email address for login (this must be the same email address used to register) to subscribe@both.org with “Subscribe” as the subject line.

You’ll receive a confirmation of your subscription with your initial password as soon as we are able to process it.

Administration

  • Log in
  • Entries feed
  • Comments feed
  • WordPress.org

License and AI Statements

Both.org aims to publish everything under a Creative Commons Attribution ShareAlike license. Some items may be published under a different license. You are responsible to verify permissions before reusing content from this website.

The opinions expressed are those of the individual authors, not Both.org.

You may not use this content to train AI.

 

Advertising Statement

Both.org does not sell advertising on this website.


Advertising may keep most websites running—but at Both.org, we’re committed to keeping our corner of the web ad-free. Both.org does not sell advertising on the website. Nor do we offer sponsored articles at this time. We’ll update this page if our position on sponsorships changes.

We want to be open about how the website is funded. Both.org is supported entirely by David Both and a few other dedicated individuals.

 

 

Copyright © All rights reserved. | MoreNews by AF themes.