Operations 6 min read

Master Linux Cron: Create, Manage, and Debug Scheduled Tasks

This guide explains how to use Linux crontab for scheduling tasks, covering basic job creation, locating user and system crontab files, enabling cron logging, controlling user access, command options, time syntax, common scheduling patterns, and editing the system-wide /etc/crontab file.

Raymond Ops
Raymond Ops
Raymond Ops
Master Linux Cron: Create, Manage, and Debug Scheduled Tasks

Creating a Simple Scheduled Task

Run a command every minute that appends the current date to /home/xxx/time.log:

$ crontab -e
* * * * * echo `date` >> /home/xxx/time.log

The crontab executable resides at /usr/bin/crontab. User crontabs are stored in /var/spool/cron/crontabs/ and require root permission to access. Each file named after a user contains that user's scheduled jobs. Do not edit these files directly with vi.

Cron logs are written to /var/log/cron, but Ubuntu disables this by default. Enable logging by editing the rsyslog configuration: $ sudo vim /etc/rsyslog.d/50-default.conf Uncomment the line: cron.* /var/log/cron.log Then restart rsyslog: $ sudo service rsyslog restart Afterward, /var/log/cron will contain cron execution logs, which are essential for troubleshooting.

Controlling User Access to Cron

To deny a user from running cron jobs, add the username (one per line) to /etc/cron.deny. To explicitly allow users, use /etc/cron.allow, which takes precedence over /etc/cron.deny. It is recommended to use only one of these files to avoid confusion.

Cron Command Options

-u

: Only root can use this to manage other users' crontabs. -e: Edit the current crontab. -l: List the crontab entries. -r: Remove all crontab entries (use -e to edit specific lines).

Time Format

The cron schedule consists of five fields: minute, hour, day of month, month, day of week, followed by the command. # minute hour day month weekday command Special characters: * – any value. , – value list separator. - – range of values. /n – step values (every n units).

Common Scheduling Examples

Run on May 1st at 10:05 each year: 5 10 1 5 * command Run at 03:00 and 06:00 daily: 0 3,6 * * * command Run at 08:20, 09:20, 10:20, 11:20 daily: 20 8-11 * * * command Run every 5 minutes: */5 * * * * command Run every Monday at 10:00: * 10 * * 1 command Run every minute: * * * * * command Run hourly at minute 0: 0 * * * * command Run daily at midnight: 0 0 * * * command Run monthly on the 1st at midnight:

0 0 1 * * command

Configuring System‑Level Scheduled Tasks

While crontab -e edits user‑level jobs, system‑wide jobs are defined in /etc/crontab. Edit this file with root privileges, for example using vim /etc/crontab:

Editing /etc/crontab
Editing /etc/crontab
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

croncrontabScheduled Tasks
Raymond Ops
Written by

Raymond Ops

Linux ops automation, cloud-native, Kubernetes, SRE, DevOps, Python, Golang and related tech discussions.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.