Skip to main content

Command Palette

Search for a command to run...

Weekly Script Checker

Published
2 min read

Not every script needs to run daily. Some just need to run once a week, but reliably without cluttering your crontab or running when nothing's changed.

So I wrote a simple Bash script that:

  • Tracks when a script was last run

  • Checks if it's been more than 7 days

  • If yes: runs it and logs the time

  • If not: exits silently


The Problem

I wanted my script to be smart enough to only run if it hadn’t already run recently, but I didn’t want to mess around with cron's date math or build external logging.

All I really needed was:

  1. A timestamp file

  2. A check against current time

  3. A 7-day timeout


✅ The Solution

Here’s the script I wrote:

#!/bin/bash

# Path to the timestamp file
TIMESTAMP_FILE="/tmp/script_last_run"

# Path to script.sh
SCRIPT="/path/to/script.sh"  # Update this to your real script path

# Current time in seconds since epoch
CURRENT_TIME=$(date +%s)

# If timestamp file exists, compare time
if [[ -f "$TIMESTAMP_FILE" ]]; then
    LAST_RUN_TIME=$(cat "$TIMESTAMP_FILE")
    TIME_DIFF=$((CURRENT_TIME - LAST_RUN_TIME))

    # 604800 seconds = 7 days
    if (( TIME_DIFF < 604800 )); then
        exit 0  # It's been less than 7 days — do nothing
    fi
fi

# Otherwise: record this run and launch the target script
echo "$CURRENT_TIME" > "$TIMESTAMP_FILE"
bash "$SCRIPT"

How to Use It

  1. Replace /path/to/script.sh with your actual script path.

  2. Call this wrapper script from your daily cronjob, systemd timer, or even a manual workflow.

  3. That’s it. It’ll only run the real job script once a week.


Example Cron Entry

@daily /home/roy/scripts/runner.sh

This will check daily, but only actually run script.sh if 7 days have passed. You can think of it as a built-in cooldown timer.


Why Bother?

Because in automation, running too often is just as bad as not running at all. This lightweight pattern:

  • Prevents redundant scraping

  • Avoids hitting servers unnecessarily

  • Keeps your system clean and efficient

  • Makes your logs more meaningful

TL;DR:

This script runs your job only once per week, even if called daily.