Resources Infrastructure

How to Edit Crontab: A Guide to Managing Scheduled Jobs

How to edit a crontab with `crontab -e`, change the default editor, list and remove entries, avoid common mistakes, and when to move production schedules to an orchestrator.

TL;DR — Run crontab -e to edit your user’s cron table in the editor set by EDITOR, crontab -l to list it, and crontab -r to delete it. Each line is a five-field schedule followed by a command. Cron suits simple jobs on one machine; production schedules need retries, alerts, and history.

For decades, cron has been the go-to utility for scheduling automated tasks on Unix-like systems. Its simplicity makes it a good fit for basic, repetitive jobs, from system maintenance scripts to routine backups. As infrastructure grows and those jobs become business-critical, its limits start to show.

This guide will walk you through the essentials of editing crontab entries, mastering syntax, and troubleshooting common issues. It then covers where cron falls short in production and how to run the same schedules in an orchestrator such as Kestra.

How Crontab Editing Works

The primary way to interact with your scheduled jobs is through the crontab command. Each user on a system can have their own crontab file, stored under /var/spool/cron/crontabs on Debian and Ubuntu or /var/spool/cron on Red Hat based systems. Do not edit these files directly; use the crontab command, which validates the file and notifies the cron daemon.

To edit your user’s crontab, run:

crontab -e

This command opens the crontab file in your system’s default text editor. If you’re a first-time user on some systems, you might be prompted to choose an editor like nano or vim. Many users find themselves unexpectedly in vim, which has a steeper learning curve.

To change the editor for the current session, you can set the EDITOR environment variable before running the command:

export EDITOR=nano
crontab -e

To make this change permanent, add the export EDITOR=nano line to your shell’s configuration file, such as ~/.bashrc, ~/.zshrc, or ~/.profile.

Other essential crontab commands include:

  • crontab -l: Lists the current user’s cron jobs.
  • crontab -r: Removes the current user’s entire crontab file. Use this with extreme caution: by default there is no confirmation prompt (some implementations offer -i to ask first). Run crontab -l > crontab.bak before editing to keep a backup.
  • sudo crontab -u <username> -e: Edits the crontab for another user, assuming you have the necessary root permissions.

Understanding Crontab Syntax and Common Schedules

A crontab entry consists of two parts: the schedule expression and the command to run. The schedule is defined by five fields, documented in crontab(5):

┌───────────── minute (0 - 59)
│ ┌───────────── hour (0 - 23)
│ │ ┌───────────── day of month (1 - 31)
│ │ │ ┌───────────── month (1 - 12)
│ │ │ │ ┌───────────── day of week (0 - 6) (Sunday to Saturday)
│ │ │ │ │
* * * * * /path/to/command

Here are some common scheduling patterns:

  • Every 15 minutes:
    */15 * * * * /path/to/your/script.sh
  • Every 30 minutes:
    */30 * * * * /path/to/your/script.sh
  • Every 2 hours:
    0 */2 * * * /path/to/your/script.sh
  • Once a day at midnight:
    0 0 * * * /path/to/your/script.sh

Cron expressions cover fixed intervals well. Cronie’s CRON_TZ sets one time zone for a whole crontab, not per job; per-job time zones, dependencies between jobs, or calendars with exceptions need more than cron syntax. The cron replacement guide compares the options.

Why a Cron Job Does Not Run

  • Minimal environment: cron does not load your shell profile, so a command that is not on its short default PATH fails. Use absolute paths or set PATH= at the top of the crontab.
  • Unescaped %: cron turns % in the command into a newline and sends the rest to the command as standard input. Escape it as \%, for example in date +\%F.
  • Missing final newline: each entry must end with a newline, or cron may treat the crontab as broken.
  • Script not executable: run chmod +x on the script and start it with a shebang such as #!/bin/bash.
  • Where to look: grep CRON /var/log/syslog on Debian and Ubuntu, /var/log/cron on Red Hat based systems, or journalctl -u cron (-u crond on Red Hat). Without a redirect, the job’s output goes to local mail (MAILTO).
  • Test before installing: cronie’s crontab -T file checks the syntax without installing the file.

Ready to orchestrate your infrastructure?

Why Reliable Orchestration Beyond Crontab is Essential

crontab is enough for simple tasks, but it falls short in production, where reliability and maintainability matter.

  • Lack of Dependency Management: Cron jobs are isolated. There is no built-in way to define that jobB should only run after jobA succeeds.
  • Poor Error Handling and Recovery: cron mails a job’s output to the crontab owner (or to MAILTO); on servers with no mail transfer agent, that output is discarded, so failures go unnoticed unless you redirect to a log. There are no automatic retries or alerting mechanisms.
  • Limited Observability: Troubleshooting requires manually searching through system logs. There is no centralized dashboard to view execution history, logs, or performance metrics. This contrasts sharply with modern workflow observability platforms.
  • Version Control Challenges: Crontab files are often edited directly on servers, making it difficult to track changes, review updates, or roll back to a previous version. This is a significant departure from GitOps best practices.
  • Complex Permissions and Environments: cron runs jobs with a minimal environment (SHELL=/bin/sh, a short PATH, HOME and LOGNAME from /etc/passwd, no ~/.bashrc), so a script that works in your shell can fail under cron. Managing paths and permissions for different users becomes a maintenance burden.
  • Silently Skipped Runs: If the cron host is down, the job is silently skipped. Kestra keeps the schedule off that host, so a missed run shows up as a failed execution and an alert instead of nothing.

These limitations lead many teams to a dedicated job scheduler or a full orchestration platform.

Orchestrate Scheduled Tasks with Kestra: An Advanced Approach

Instead of scattering crontab entries across multiple servers, you can define, monitor, and manage all your scheduled tasks in one place with Kestra.

Consider a simple daily cleanup script. In a crontab, it might look like this: 0 2 * * * /path/to/cleanup.sh >> /var/log/cleanup.log 2>&1

Here is the same job in Kestra. The script stays on the server and runs over SSH, so nothing needs to be rewritten:

id: daily-cleanup
namespace: company.team.ops
triggers:
- id: daily_schedule
type: io.kestra.plugin.core.trigger.Schedule
cron: "0 2 * * *"
tasks:
- id: run_cleanup_script
type: io.kestra.plugin.fs.ssh.Command
host: ops-server.internal
authMethod: PUBLIC_KEY
username: ops
privateKey: "{{ secret('OPS_SSH_KEY') }}"
commands:
- /path/to/cleanup.sh
retry:
type: constant
maxAttempts: 3
interval: PT10M
errors:
- id: send_failure_alert
type: io.kestra.plugin.slack.notifications.SlackIncomingWebhook
url: "{{ secret('SLACK_WEBHOOK_URL') }}"
payload: |
{"text": "Daily cleanup failed after retries (flow {{ flow.id }}, execution {{ execution.id }})."}

The schedule is the same cron expression, and the trigger’s timezone property sets its time zone per flow. Other differences:

  • Declarative and Version-Controlled: The entire workflow is a YAML file that can be stored in Git, reviewed, and versioned.
  • Built-in Retries: The retry block automatically re-runs the script if it fails, preventing transient issues from causing a permanent failure.
  • Automatic Error Handling: The errors block runs only on failure and sends an alert to Slack with the execution ID.
  • Centralized Observability: All executions, logs, and outcomes are visible in the Kestra UI, providing a complete audit trail.
  • Missed runs are visible: the schedule lives in Kestra, not on ops-server.internal, so if that host is down the SSH task fails, retries, and the alert fires instead of the run being silently skipped.

To learn more about scheduling in Kestra, check the Schedule trigger documentation. For a detailed walkthrough, see our guide on how to migrate from cron to Kestra.

Terraform, Ansible and Kubernetes, orchestrated from one place.

Frequently asked questions

Find answers to your questions right here, and don't hesitate to Contact Us if you couldn't find what you're looking for.