“TeckMile.com – Elevating Your Skills and Creativity, One Project at a Time.”

A Guide to Crontabs: Scheduling Tasks

Crontabs are a powerful way to schedule and automate tasks in Linux. In this guide, we’ll cover how to create, manage, and remove scheduled tasks using cron.

Viewing Your Cron Table

To view your existing cron table, use the following command:

crontab -l

Setting Your Default Editor

You can choose your preferred text editor for working with crontabs. For example, to make nano your default editor, run:

export EDITOR=/usr/bin/nano

Creating or Editing Your Cron Table

To create or edit your crontab, use:

crontab -e

Cron Format

Cron expressions follow a specific format:

* * * * * command_to_execute

  • *: Wildcard, meaning “every.”
  • min: Minute (0 – 59).
  • hr: Hour (0 – 23).
  • day: Day of the month (1 – 31).
  • mnth: Month (1 – 12).
  • dayofweek: Day of the week (0 – 7, where both 0 and 7 represent Sunday).

Examples

  1. To execute a command at 01:32 on the 17th of January with no day-of-week: 32 01 17 01 * command_to_execute
  2. To execute a command at 20:14 every Monday in March: 14 20 * 03 1 command_to_execute
  3. Days can also be expressed as abbreviations e.g. ‘mon’ instead of ‘1’, making the command more readable: 14 20 * 03 mon command_to_execute

Running a Script

You can also set the path to a script in your crontab. For example:

32 01 17 01 * /path/to/your/script.sh

Make sure the script is executable (chmod +x /path/to/your/script.sh).

Removing a Crontab

You can remove your crontab with:

crontab -r

Editing Another User’s Crontab

To edit another user’s crontab, use sudo and specify the user:

sudo crontab -u root -e

Or, without sudo:

crontab -u apache


Comments

2 responses to “A Guide to Crontabs: Scheduling Tasks”

  1. Its like you read my thoughts! You seem to know a lot approximately this, like you wrote the book in it or something. I feel that you simply can do with a few percent to power the message home a bit, however other than that, this is magnificent blog. A fantastic read. I’ll certainly be back.

    1. Hi there, Thanks for the kind words, I will take that on board!

Leave a Reply

Your email address will not be published. Required fields are marked *