How to Schedule Hourly Cron Jobs on Ubuntu: Syntax, Troubleshooting & Best Practices


4 views

The correct syntax for running a script every hour in Linux cron is:

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

When your hourly cron job isn't working, check these aspects:

  • PATH issues: Cron has minimal PATH. Always use full paths to binaries and scripts
  • Permission problems: The script must be executable (chmod +x)
  • Environment variables: Cron doesn't load your shell environment
  • Output handling: Cron emails output by default. Redirect to a log file:
0 * * * * /path/to/script.sh >> /var/log/cron.log 2>&1

Here's how to systematically troubleshoot:

  1. Check cron logs:
    sudo grep CRON /var/log/syslog
    
  2. Test script execution manually:
    sudo -u [cron_user] /path/to/script.sh
    

For Ubuntu systems, the best practice is to:

# Edit crontab for current user
crontab -e

# Add this entry for hourly execution
0 * * * * /usr/bin/env bash /home/user/scripts/hourly_task.sh >> /home/user/cron.log 2>&1

For system-wide hourly jobs (requires root):

sudo nano /etc/crontab
# Add:
0 *    * * *   root    /usr/local/bin/hourly_maintenance.sh

On Ubuntu/Debian systems, you can drop scripts here:

sudo cp my_hourly_script /etc/cron.hourly/
sudo chmod +x /etc/cron.hourly/my_hourly_script
  • Never run cron jobs as root unless absolutely necessary
  • Validate all script inputs when running via cron
  • Consider using flock to prevent concurrent execution
0 * * * * /usr/bin/flock -n /tmp/hourly.lock /path/to/script.sh

The fundamental syntax for an hourly cron job in Ubuntu/Linux is:

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

This means:

  • 0 - At minute 0
  • * - Every hour
  • * - Every day
  • * - Every month
  • * - Every day of the week

1. Environment Variables Missing

Cron runs with a minimal environment. Always specify full paths:

0 * * * * /bin/bash /home/user/scripts/backup.sh

2. Permission Issues

Check these three permissions:

# Make script executable
chmod +x /path/to/script.sh

# Verify cron service is running
sudo service cron status

# Check user crontab permissions
ls -l /var/spool/cron/crontabs/

3. Output Not Captured

Redirect output to debug:

0 * * * * /path/to/script.sh > /tmp/cron.log 2>&1

Checking Cron Logs

Ubuntu logs cron activity here:

sudo tail -f /var/log/syslog | grep CRON

Testing with Simple Commands First

Validate your cron setup with a test command:

* * * * * /usr/bin/touch /tmp/cron_test_$(date +\%s)

Wait 1 minute and check if the file appears in /tmp/

Handling Complex Scripts

For Python/Ruby/etc. scripts, specify the interpreter:

0 * * * * /usr/bin/python3 /home/user/scripts/hourly_task.py

Example timer unit file:

[Unit]
Description=Run hourly backup

[Timer]
OnCalendar=*-*-* *:00:00
Persistent=true

[Install]
WantedBy=timers.target

Save as /etc/systemd/system/hourly-backup.timer

  1. Check cron syntax: crontab -l
  2. Verify script runs manually first
  3. Test with immediate timestamp: * * * * *
  4. Confirm in logs: grep CRON /var/log/syslog