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.
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.logThe 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 * * commandConfiguring 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:
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Raymond Ops
Linux ops automation, cloud-native, Kubernetes, SRE, DevOps, Python, Golang and related tech discussions.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
