Master Linux Scheduling: Create and Manage Crontab Jobs Like a Pro
This guide walks you through the fundamentals of using Linux crontab, from creating simple per‑minute tasks and configuring system‑wide jobs to controlling user access, understanding time syntax, and enabling cron logging for effective troubleshooting.
Having been accustomed to Windows Task Scheduler, you may find using Linux's crontab for managing scheduled tasks unfamiliar.
Below is a concise summary of the basic usage.
Create a Simple Scheduled Task
Output the current time every minute to a time.log file in the user's home directory.
$ crontab -e</code>
<code>* * * * * echo `date` >> /home/xxx/time.logThe crontab executable resides at /usr/bin/crontab.
User-specific cron jobs are stored under /var/spool/cron/crontabs/; root privileges are required to access this directory, and each file named after a user contains that user's jobs. Do not edit these files directly with vi.
Cron activity is logged to /var/log/cron, but Ubuntu disables this log by default.
To enable logging, edit the rsyslog configuration file /etc/rsyslog.d/50-default.conf, uncomment the line: cron.* /var/log/cron.log Then restart rsyslog: $ sudo service rsyslog restart Afterwards, /var/log/cron will be available, providing a crucial source for diagnosing cron job issues.
Control User Access to Cron Jobs
To prevent a specific user from running cron jobs, add the username (one per line) to /etc/cron.deny. Alternatively, explicitly allow users by listing them in /etc/cron.allow, which takes precedence over /etc/cron.deny.
crontab Command Options
-u: Only root can use this to create or remove cron jobs for other users. -e: Edit the crontab file. -l: List the current crontab entries. -r: Remove all crontab entries (use -e to edit a single entry instead).
Time Format
Fields: minute hour day-of-month month day-of-week command
Numeric ranges: 0‑59 (minute), 0‑23 (hour), 1‑31 (day), 1‑12 (month), 0‑7 (day of week, where both 0 and 7 represent Sunday).
Special characters: * – any value , – value list separator - – range of values /n – step values (every n units)
Common Scheduling Examples
Case 1: Run once on May 1st at 10:05 each year 5 10 1 5 * command Case 2: Run at 3 AM and 6 AM daily 0 3,6 * * * command # note the comma Case 3: Run at 8:20, 9:20, 10:20, and 11:20 each day 20 8-11 * * * command # note the dash Case 4: Run every five minutes */5 * * * * command # note the /n Case 5: Run every Monday at 10 AM * 10 * * 1 command Case 6: Run every minute * * * * * command Case 7: Run hourly 0 * * * * command Case 8: Run once daily at midnight 0 0 * * * command Case 9: Run on the first day of each month at midnight
0 0 1 * * commandConfigure System‑Level Cron Jobs
While crontab -e edits user‑level jobs, system‑wide jobs are defined in /etc/crontab and require root privileges.
Example: Open the file with vim for editing.
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.
MaGe Linux Operations
Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.
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.
