How to Set Up a Biweekly Cron Job on Saturday at 8 AM: Complete Guide with Examples


2 views

To schedule a job to run every two weeks on Saturday at 8 AM, we need to carefully construct the cron expression. The standard cron format consists of five fields:

* * * * *
- - - - -
| | | | |
| | | | +----- Day of week (0 - 6) (Sunday=0)
| | | +------- Month (1 - 12)
| | +--------- Day of month (1 - 31)
| +----------- Hour (0 - 23)
+------------- Minute (0 - 59)

For your specific requirement (every other Saturday at 8 AM), the cron expression would be:

0 8 * * 6 [ $(expr $(date +\%W) \% 2) -eq 0 ] && command-to-execute

Alternatively, in modern cron implementations like Vixie cron, you can use:

0 8 */14 * 6 command-to-execute

Here's how you would add this to your crontab:

# Edit crontab
crontab -e

# Add this line for biweekly Saturday execution at 8 AM
0 8 * * 6 [ $(expr $(date +\%W) \% 2) -eq 0 ] && /path/to/your/script.sh

For systems using systemd, you can create a more precise biweekly timer:

[Unit]
Description=Run biweekly job on Saturday

[Timer]
OnCalendar=Sat *-*-* 08:00:00
Persistent=true
Unit=biweekly-job.service

[Install]
WantedBy=timers.target

To verify your cron job will run on the correct dates, use this testing command:

for i in {1..12}; do date -d "now + $i weeks"; done | grep "Sat" | awk 'NR % 2 == 1'

Remember that cron jobs run in the system's timezone. To explicitly set a timezone:

0 8 * * 6 [ $(TZ=Your/Timezone expr $(date +\%W) \% 2) -eq 0 ] && command

Always implement logging for your cron jobs to track execution:

0 8 * * 6 [ $(expr $(date +\%W) \% 2) -eq 0 ] && /path/to/script.sh >> /var/log/biweekly.log 2>&1

When you need to schedule a task to run every two weeks on a specific day (Saturday) at a fixed time (8 AM), cron is the perfect tool for the job. Cron is a time-based job scheduler in Unix-like operating systems, and it allows you to automate repetitive tasks with precision.

The standard cron syntax consists of five fields:

* * * * * command_to_execute
┬ ┬ ┬ ┬ ┬
│ │ │ │ └── Day of the week (0 - 6) (0 is Sunday)
│ │ │ └──── Month (1 - 12)
│ │ └────── Day of the month (1 - 31)
│ └──────── Hour (0 - 23)
└────────── Minute (0 - 59)

To run a job every two weeks on Saturday at 8 AM, you can use either of these approaches:

Method 1: Using Day of Month and Day of Week

0 8 1-7,15-21 * 6 /path/to/your/script.sh

This cron expression will run:

  • On Saturdays (6)
  • At 8 AM (0 8)
  • Only during the 1st-7th and 15th-21st days of each month

Method 2: Using a Wrapper Script

Create a wrapper script that checks the week number:

#!/bin/bash
# /path/to/wrapper.sh

WEEK=$(date +\%U)
if [ $((WEEK\%2)) -eq 0 ]; then
    /path/to/your/actual/script.sh
fi

Then set up a simpler cron job:

0 8 * * 6 /path/to/wrapper.sh

Always verify your cron jobs with:

# List current cron jobs
crontab -l

# Test your cron expression
# Install cron tester if needed
sudo apt-get install cron-apt

For modern Linux systems, consider using systemd timers:

# /etc/systemd/system/biweekly-script.timer
[Unit]
Description=Run script every two weeks on Saturday

[Timer]
OnCalendar=Sat *-*-* 08:00:00
Persistent=true
Unit=biweekly-script.service

[Install]
WantedBy=timers.target
  • Remember cron uses the server's timezone
  • Ensure your script has executable permissions
  • Capture output by redirecting stdout/stderr in your cron job

For the specific case mentioned (starting October 4, 2014), this cron job would work:

0 8 4,18 10,11 * /path/to/your/script.sh

For a permanent every-other-Saturday solution, combine with the wrapper script approach for reliability.