The Ultimate Crontab Guide: Mastering Scheduling from Hourly to Every N Hours
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
The Ultimate Guide to Docker Cron Logging: Host vs. Container Redirection - Are You Doing It Right?
Duration: 00:00 | DP | 2026-01-05 08:03:52Linux Command-Line Mystery: Why `ll` Hides Files like `.idea` & The Ultimate `ls` vs. `ll` Showdown
Duration: 00:00 | DP | 2025-12-01 08:08:00Shell Magic: How to Gracefully Write Output from Multiple Commands to a Single Log File
Duration: 00:00 | DP | 2025-12-17 04:10:504 Command-Line Tricks to Quickly Find Your NFS Mount Point
Duration: 00:00 | DP | 2025-11-22 17:29:05The Ultimate Guide to the Linux `cp` Command: Avoiding Common Copying Pitfalls
Duration: 00:00 | DP | 2025-12-23 19:36:40The Ultimate Guide to Linux `rm` Command: How to Safely and Efficiently Delete Directories
Duration: 00:00 | DP | 2025-12-24 07:52:30The Ultimate Guide to Linux File Permissions: From `chmod 644` to the Mysterious `@` Symbol
Duration: 00:00 | DP | 2025-12-25 08:24:10Linux Command-Line Magic: 3 Ways to Instantly Truncate Large Files
Duration: 00:00 | DP | 2025-12-27 21:43:20From Concept to Cron Job: Building the Perfect SEO Sitemap for a Multilingual Video Website
Duration: 00:00 | DP | 2026-01-20 08:23:13The Ultimate Guide to Docker Cron Jobs: Effortlessly Scheduling PHP Tasks in Containers from the Host
Duration: 00:00 | DP | 2025-12-29 10:30:50From Phantom Conflicts to Docker Permissions: A Deep Dive into Debugging an Infinite Loop in a Git Hook for an AI Assistant
Duration: 00:00 | DP | 2025-11-09 16:39:00Building a Bulletproof PHP Analytics System: From DB Schema to Self-Healing Cron Jobs
Duration: 00:00 | DP | 2025-11-10 01:03:00Master Batch File Creation in Linux: 4 Efficient Command-Line Methods
Duration: 00:00 | DP | 2025-11-10 09:27:00PHP CLI Magic: 3 Ways to Run Your Web Scripts from the Command Line with Parameters
Duration: 00:00 | DP | 2025-11-11 19:03:00Crontab Logs Missing Dates? 4 Practical Ways to Easily Add Timestamps
Duration: 00:00 | DP | 2025-11-12 03:27:00Decoding the 99% I/O Wait: The Ultimate Post-Mortem Guide for CentOS Server 'Freezes'
Duration: 00:00 | DP | 2025-12-31 23:50:00How to Automatically Run Git Clone on Docker Start? 3 Practical Methods Explained
Duration: 00:00 | DP | 2026-02-15 13:47:17How to Easily Fix the "error: externally-managed-environment" in Python
Duration: 00:00 | DP | 2026-01-29 08:34:50Recommended
How to Add Port Mappings to a Running Docker Container: 3 Proven Methods
00:00 | 16In development and operations, it's a common scena...
Cracking the TypeScript TS2339 Puzzle: Why My Vue ref Became the `never` Type
00:00 | 51Ever encountered the tricky `Property '...' does n...
One-Liner PHP Magic: Securely Filter Arrays with `array_intersect_key` and `array_flip`
00:00 | 42Discover the powerful combination of `array_inters...
The Magic of Hex Random Strings: From UUIDs to API Keys, Why Are They Everywhere?
00:00 | 46Have you ever been curious about cryptic strings l...