The Ultimate Crontab Guide: Mastering Scheduling from Hourly to Every N Hours

Published: 2026-03-11
Author: DP
Views: 0
Category: Linux
Content
## The Scenario When working with Linux systems, we often need to set up scheduled tasks to automate scripts or commands. A common question arises: how can we precisely control the execution frequency? For instance, does the rule `16 * * * *` actually run a job every hour? And how would one modify it to run every 2 or 3 hours? This article starts with this very problem and provides a comprehensive guide to using Crontab. --- ## Answering the Core Questions ### 1. Does `16 * * * *` Run Once Every Hour? **Yes, it absolutely does.** This expression means: "Execute the task at the **16th minute** of **every hour**." The asterisk `*` in the hour field acts as a wildcard for "every possible value," so the task will run at 00:16, 01:16, 02:16, and so on, up to 23:16, ensuring an hourly execution. ### 2. How to Set Up a Job for Every 2 or 3 Hours? To achieve an execution interval like "every N units," we use the slash `/` character, which defines a "step" value. * **Run at the 16th minute of every 2 hours**: ```crontab 16 */2 * * * docker exec your-container php /var/www/wiki.lib00.com/task.php ``` This sets the step for the hour field to 2, causing the job to run at 00:16, 02:16, 04:16, ..., 22:16. * **Run at the 16th minute of every 3 hours**: ```crontab 16 */3 * * * docker exec your-container php /var/www/wiki.lib00.com/task.php ``` This sets the step for the hour field to 3, causing the job to run at 00:16, 03:16, 06:16, ..., 21:16. --- ## A Deep Dive into Crontab Syntax The basic Crontab format consists of five time fields followed by the command to be executed, which together define the schedule. ```plaintext .---------------- minute (0 - 59) | .------------- hour (0 - 23) | | .---------- day of month (1 - 31) | | | .------- month (1 - 12) | | | | .---- day of week (0 - 6) (Sunday is both 0 and 7) | | | | | * * * * * command_to_execute ``` ### Understanding Special Characters | Character | Name | Meaning & Example | | :--- | :--- | :--- | | `*` | Asterisk (Wildcard) | Represents **all possible values** for the field. E.g., `*` in the hour field means "every hour." | | `,` | Comma | Separates **multiple discrete values**. E.g., `0,15,30,45` in the minute field means "at minutes 0, 15, 30, and 45." | | `-` | Hyphen | Defines a **continuous range** of values. E.g., `9-17` in the hour field means "from 9 AM to 5 PM." | | `/` | Slash (Step) | Specifies an **interval**. E.g., `*/10` in the minute field means "every 10 minutes." | --- ## Common & Practical Examples 1. **Run every minute** (useful for testing or high-frequency monitoring) ```crontab * * * * * /opt/scripts/lib00/heartbeat.sh ``` 2. **Run a backup at 2:30 AM every day** ```crontab 30 2 * * * /opt/scripts/wiki.lib00/backup.sh ``` 3. **Run at 9:00 AM every weekday (Monday to Friday)** ```crontab 0 9 * * 1-5 /opt/scripts/wiki.lib00.com/start_work.sh ``` 4. **Run at 5:00 PM every weekend (Saturday and Sunday)** ```crontab 0 17 * * 6,0 /opt/scripts/lib00/weekend_report.sh ``` 5. **Run every 10 minutes** ```crontab */10 * * * * /usr/bin/php /var/www/wiki.lib00.com/check_status.php ``` 6. **Run a monthly report at 1:00 AM on the 1st of every month** ```crontab 0 1 1 * * /opt/scripts/monthly_report.sh ``` 7. **Run at the top and bottom of every hour (minute 0 and 30)** ```crontab 0,30 * * * * /opt/scripts/half_hour_sync.sh ``` --- ## Pro Tips from DP * **Use Absolute Paths**: The execution environment for Crontab is minimal and often doesn't load a user's `.bashrc` or `.profile` files. To prevent errors from commands or scripts not being found, always use their absolute paths. * **Redirect Output**: By default, any output from a cron job is emailed to the user. To avoid spamming your inbox and to manage logs effectively, it's best practice to redirect the output. * **To discard all output**: `>/dev/null 2>&1` * **To append to a log file**: `>> /var/log/lib00_cron.log 2>&1` **Example with logging**: ```crontab 16 */2 * * * docker exec your-container php /var/www/wiki.lib00.com/task.php >> /var/log/lib00_cron.log 2>&1 ``` By following these guidelines, you can manage your scheduled tasks in a more robust and professional manner.
Related Contents