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:
<code>$ crontab -e
* * * * * echo `date` >> /home/xxx/time.log</code>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:
<code>$ sudo vim /etc/rsyslog.d/50-default.conf</code>Uncomment the line:
<code>cron.* /var/log/cron.log</code>Then restart rsyslog:
<code>$ sudo service rsyslog restart</code>Afterward,
/var/log/cronwill 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
-eto 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.
<code># minute hour day month weekday command</code>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:
<code>5 10 1 5 * command</code>Run at 03:00 and 06:00 daily:
<code>0 3,6 * * * command</code>Run at 08:20, 09:20, 10:20, 11:20 daily:
<code>20 8-11 * * * command</code>Run every 5 minutes:
<code>*/5 * * * * command</code>Run every Monday at 10:00:
<code>* 10 * * 1 command</code>Run every minute:
<code>* * * * * command</code>Run hourly at minute 0:
<code>0 * * * * command</code>Run daily at midnight:
<code>0 0 * * * command</code>Run monthly on the 1st at midnight:
<code>0 0 1 * * command</code>Configuring System‑Level Scheduled Tasks
While
crontab -eedits user‑level jobs, system‑wide jobs are defined in
/etc/crontab. Edit this file with root privileges, for example using
vim /etc/crontab:
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.