Troubleshooting Crontab Syntax: Why */2 Runs Every 2 Minutes Instead of 2 Hours


9 views

When setting up a cron job to run every 2 hours, many developers make this common syntax error:

* */2 * * * /path/to/script.sh

This actually runs every minute (first asterisk) during every 2nd hour (second field). The correct syntax should be:

0 */2 * * * /path/to/script.sh

Let's examine the correct fields for a 2-hour interval:

  • 0 - Runs at minute 0
  • */2 - Runs every 2 hours
  • * - Runs every day
  • * - Runs every month
  • * - Runs every day of week

Here are three equally valid ways to schedule every 2 hours:

0 0,2,4,6,8,10,12,14,16,18,20,22 * * * /path/to/script.sh
0 */2 * * * /path/to/script.sh
0 0-23/2 * * * /path/to/script.sh

To troubleshoot cron issues, check these locations:

  • System logs: grep CRON /var/log/syslog
  • User-specific logs: Check /var/mail/$USER
  • Command output: Redirect output in your cron job:
0 */2 * * * /path/to/script.sh >> /var/log/cron.log 2>&1

Other reasons your cron might not work:

  • Environment variables not being set (use full paths)
  • Script permissions issues (ensure executable bit is set)
  • Shell differences (specify shell in crontab: SHELL=/bin/bash)
  • Newline requirement (crontab must end with newline)

Many sysadmins encounter this issue when first configuring cron jobs. The incorrect syntax:

* */2 * * * /path/to/script.sh

Actually means "run every minute when the hour is divisible by 2" rather than "run every 2 hours".

For true 2-hour intervals, you need either:

0 */2 * * * /path/to/script.sh

Or the more explicit:

0 0,2,4,6,8,10,12,14,16,18,20,22 * * * /path/to/script.sh

When troubleshooting cron issues:

# Check cron logs (system location may vary)
sudo tail -f /var/log/syslog | grep CRON

# Verify environment variables
env > /tmp/cronenv.log

# Test command execution manually
/path/to/script.sh > /tmp/crontest.log 2>&1
  • Missing PATH in cron environment
  • Relative paths in scripts
  • Permission issues on scripts
  • Not capturing stdout/stderr

For systems that may be offline during execution windows:

# Example anacrontab entry
2  5  cron.daily  /path/to/script.sh