How to Set Up a Cron Job for Weekdays (Mon-Fri) at 8 AM in Ubuntu 16.04


2 views

When setting up a cron job to run at 8:00 AM on workdays (Monday through Friday) in Ubuntu 16.04, the correct syntax should theoretically be either:

0 8 * * 1-5 /path/to/command

or

0 8 * * MON-FRI /path/to/command

Several factors could cause your cron job to fail:

  • The cron service might not be running (sudo service cron status)
  • Environment variables might be missing in the cron context
  • File permissions issues with the script
  • Incorrect path specifications

First check if the cron job is properly registered:

crontab -l

To view cron logs for debugging:

grep CRON /var/log/syslog

Try this more explicit version that includes full paths:

0 8 * * 1-5 /bin/bash /full/path/to/your_script.sh >> /var/log/cron.log 2>&1

First verify cron works with a simple test:

* * * * * echo "Cron is working $(date)" >> /tmp/cron_test.log

Wait a minute and check the log file:

cat /tmp/cron_test.log

Here's a complete example that includes logging:

0 8 * * 1-5 /usr/bin/python3 /home/user/scripts/daily_task.py >> /var/log/daily_task.log 2>&1

Make sure to:

  1. Make the script executable (chmod +x /path/to/script)
  2. Use absolute paths for all commands
  3. Test the command manually first

When attempting to schedule a cron job to run at 8:00 AM from Monday to Friday on Ubuntu 16.04, the correct syntax is:

0 8 * * 1-5 /path/to/your/script.sh

Alternatively, you can use:

0 8 * * MON-FRI /path/to/your/script.sh

Several factors could prevent your workday cron job from executing:

  • The system timezone might be misconfigured
  • The cron service might not be running
  • File permissions might prevent execution
  • The PATH environment variable might differ from your user's PATH

To troubleshoot, follow these steps:

# Check cron service status
sudo service cron status

# View cron logs
grep CRON /var/log/syslog

# Verify timezone
timedatectl status

# Test script execution manually
/path/to/your/script.sh

Here's a full example that emails a daily report:

# m h  dom mon dow   command
0 8 * * 1-5 /usr/bin/python3 /home/user/daily_report.py | mail -s "Daily Report" admin@example.com

For more complex scheduling needs:

# Run at 8:00 AM and 4:00 PM on workdays
0 8,16 * * 1-5 /path/to/command

# Exclude holidays using a wrapper script
0 8 * * 1-5 /home/user/run_if_not_holiday.sh /path/to/command

The wrapper script run_if_not_holiday.sh might contain:

#!/bin/bash
if ! grep -q "$(date +%Y-%m-%d)" /path/to/holidays.list; then
    "$@"
fi
  • Always use absolute paths in cron jobs
  • Redirect output to a log file for debugging
  • Consider using systemd timers for more complex schedules
  • Test your cron expressions with tools like crontab.guru