Skip to content

Instantly share code, notes, and snippets.

@Yousuf28
Created November 20, 2025 13:09
Show Gist options
  • Select an option

  • Save Yousuf28/a6b7caa9de70a7583d2da8ac4c646592 to your computer and use it in GitHub Desktop.

Select an option

Save Yousuf28/a6b7caa9de70a7583d2da8ac4c646592 to your computer and use it in GitHub Desktop.

⏱️ Running Scheduled Tasks Without Cron: A Simple Bash-Based Scheduler

A complete guide with examples, time customization, and process management

Sometimes you need to automate a task on a Linux server — but crontab isn’t available. This is common on restricted systems, containers, or environments where cron is not installed or disabled.

With a simple Bash loop, you can create your own lightweight scheduler that behaves similarly to cron.

This guide covers:

  • How to build a Bash-based scheduler
  • How to change the schedule time and day
  • Example scripts
  • Directory structure
  • How to start, stop, and restart the scheduler
  • Logging
  • Notes about server timezone differences

Note: The Linux server time may be different from your local timezone. Always verify the server time with:

date

Adjust the scheduled time accordingly.

📁 Directory Structure

your-project/
│
├── master.sh
├── run_schedule.sh
└── scheduler.log        # created after first run

📄 Example Task Script (master.sh)

This script is what you want to run on schedule.

#!/usr/bin/env bash
echo "Master script executed at $(date)" >> master.log

📄 The Scheduler Script (run_schedule.sh)

This script continuously checks the date and time. It runs master.sh every Saturday at 06:00.

#!/usr/bin/env bash
cd "$(dirname "$0")"

SCRIPT="./master.sh"

while true; do
    DOW=$(date +%u)      # Day of week (1–7, Monday=1)
    TIME=$(date +%H:%M)  # Time in 24-hour format (HH:MM)

    if [[ "$DOW" == "6" && "$TIME" == "06:00" ]]; then
        echo "Running master.sh at $(date)"
        bash "$SCRIPT"
        sleep 60  # Prevent running twice in the same minute
    fi

    sleep 20
done

🕒 How to Change the Schedule (DOW & TIME)

The scheduler uses two conditions:

  • DOW → Day of week
  • TIME → Exact time (HH:MM)

📅 Changing the Day of the Week (DOW)

The command date +%u returns:

Value Day
1 Monday
2 Tuesday
3 Wednesday
4 Thursday
5 Friday
6 Saturday
7 Sunday

Examples

Run every Monday at 06:00:

if [[ "$DOW" == "1" && "$TIME" == "06:00" ]]; then

Run every Wednesday at 06:00:

if [[ "$DOW" == "3" && "$TIME" == "06:00" ]]; then

Run every Sunday at 06:00:

if [[ "$DOW" == "7" && "$TIME" == "06:00" ]]; then

⏰ Changing the Time (TIME)

Time is checked using:

TIME=$(date +%H:%M)

This is 24-hour format.

Examples

Run at 07:30 AM:

if [[ "$TIME" == "07:30" ]]; then

Run at 11:45 PM (23:45):

if [[ "$TIME" == "23:45" ]]; then

Run at midnight:

if [[ "$TIME" == "00:00" ]]; then

🧩 Running Twice in a Day

Example: Run at 06:00 and 18:00 on Saturday:

if [[ "$DOW" == "6" && ( "$TIME" == "06:00" || "$TIME" == "18:00" ) ]]; then

🔁 Running Every Day at a Specific Time

Remove the DOW check:

if [[ "$TIME" == "06:00" ]]; then

🔄 Running Every Hour at HH:00

if [[ "$(date +%M)" == "00" ]]; then

▶️ Running the Scheduler in Background

Give execute permissions:

chmod +x master.sh
chmod +x run_schedule.sh

Start it using nohup so it survives logout:

nohup ./run_schedule.sh > scheduler.log 2>&1 &

🔍 Checking If the Scheduler Is Running

pgrep -fl run_schedule.sh

Or:

ps aux | grep run_schedule.sh

🛑 Stopping (Killing) the Scheduler

Kill by name:

pkill -f run_schedule.sh

Or kill by process ID:

ps aux | grep run_schedule.sh
kill <PID>

Force kill:

kill -9 <PID>

🔄 Restarting the Scheduler

pkill -f run_schedule.sh
nohup ./run_schedule.sh > scheduler.log 2>&1 &

📜 Viewing Logs

tail -f scheduler.log

🎉 Final Thoughts

This method lets you schedule tasks without relying on cron. By adjusting the DOW and TIME variables, you can create almost any schedule you want — daily tasks, weekly tasks, specific hours, or multiple run times.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment