Skip to main content
SysAdmin Shell Scripting Essentials

crontab every 4 hours: Syntax, Examples & Troubleshooting

crontab every 4 hour is the cron schedule expression 0 */4 * * * that triggers a job at minute 0 of every 4th hour (00:00, 04:00, 08:00, …).

# Edit current user's crontab and add a line:
crontab -e

# Each line follows: minute hour day-month month day-week command
0 */4 * * * /full/path/to/command [args]

Tested on Ubuntu 22.04 with cron 3.0pl1-136+deb11u1.

Options and Flags

Flag Type Default Description
-e none N/A Edit the current user’s crontab file in $EDITOR.
-l none N/A List the current user’s crontab entries to stdout.
-r none N/A Remove the current user’s crontab file entirely.
-u <user> string current user Operate on another user’s crontab (requires root).
0 */4 * * * expression N/A Run at minute 0 of every 4th hour (hour 0,4,8,12,16,20).

Usage Examples

Example 1: Basic every 4 hours on the hour

# Run backup at midnight, 4am, 8am, etc.
0 */4 * * * /usr/local/bin/backup.sh

The 0 */4 tells cron to execute at minute 0 of every hour that is evenly divisible by 4. This is the canonical “every 4 hours” pattern. The job will start exactly at 00:00, 04:00, 08:00, 12:00, 16:00, 20:00 daily.

See also  PowerShell tnc (Test-NetConnection) Syntax, Flags, and

Example 2: Offset start time by 30 minutes

# Run log rotation at 00:30, 04:30, 08:30, etc.
30 */4 * * * /opt/scripts/rotate_logs.sh

When the exact 4-hour alignment conflicts with other system jobs or peak load, shift the execution by replacing 0 with another minute value. The job still runs every 4 hours, but now at half past the hour.

Example 3: Every 4 hours on weekdays only

# Run monitoring checks every 4h, Mon–Fri
0 */4 * * 1-5 /usr/bin/check_health

The day-of-week field 1-5 restricts execution to Monday through Friday. Combined with */4 in the hour field, this gives 6 runs per weekday (at 00:00, 04:00, 08:00, 12:00, 16:00, 20:00). Useful for non‑critical tasks that can skip weekends.

Troubleshooting & Common Errors

Error Message / Symptom Root Cause Resolution Command
no crontab for username User has never created a crontab. crontab -e (creates a new one on save)
Job doesn’t run / MAILTO empty Syntax error in expression; missing command path. 0 */4 * * * /bin/echo "test" >> /tmp/cron.log 2>&1
/var/log/syslog: (CRON) INFO (pid1) started but no job execution cron daemon not running or crontab not reloaded. sudo service cron restart or sudo systemctl restart cron
0 */4 * * * command not found Command not in cron’s limited PATH. Use absolute path: 0 */4 * * * /usr/bin/python3 /home/user/script.py

Frequently Asked Questions

What is the difference between ‘0 */4 * * *’ and ‘*/4 * * * *’ in crontab?

Answer: ‘0 */4 * * *’ runs every 4 hours on the hour (0 min). The first field is minute. ‘0 */4’ sets minute=0, hour interval=4. ‘*/4’ in minute field triggers every 4 minutes. Correct syntax for every 4 hours: 0 */4 * * * /path/to/command

How do I fix ‘bad hour’ error when setting every 4 hours in crontab?

Answer: The ‘bad hour’ error indicates an invalid hour field value. Common mistake: ‘0-24/4’ or missing space. Correct: 0 */4 * * * /command A leading space causes error. Verify with crontab -l | head -1 and use crontab -e to edit.

Does crontab work on Windows Subsystem for Linux (WSL) for ‘every 4 hour’ schedules?

Answer: Yes, crontab works on WSL as it includes a full cron daemon. WSL systemd is not enabled by default. To persist cron: sudo crontab -e then 0 */4 * * * /path/to/script and sudo service cron start Or use task scheduler for persistent services.

What is the fastest way to create a cron job that runs every 4 hours starting at midnight?

Answer: Use crontab -e and append the line 0 */4 * * * /path/to/script. This syntax is cross-platform and requires no flags. To verify: crontab -l | grep "0 */4" For staggered start times, adjust minute field (e.g., 15 */4 * * * starts at 00:15).