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:
dateAdjust 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.logThe 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
doneHow 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" ]]; thenRun every Wednesday at 06:00:
if [[ "$DOW" == "3" && "$TIME" == "06:00" ]]; thenRun every Sunday at 06:00:
if [[ "$DOW" == "7" && "$TIME" == "06:00" ]]; thenChanging 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" ]]; thenRun at 11:45 PM (23:45):
if [[ "$TIME" == "23:45" ]]; thenRun at midnight:
if [[ "$TIME" == "00:00" ]]; thenRunning Twice in a Day
Example: Run at 06:00 and 18:00 on Saturday:
if [[ "$DOW" == "6" && ( "$TIME" == "06:00" || "$TIME" == "18:00" ) ]]; thenRunning Every Day at a Specific Time
Remove the DOW check:
if [[ "$TIME" == "06:00" ]]; thenRunning Every Hour at HH:00
if [[ "$(date +%M)" == "00" ]]; thenRunning the Scheduler in Background
Give execute permissions:
chmod +x master.sh
chmod +x run_schedule.shStart 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.shOr:
ps aux | grep run_schedule.shStopping (Killing) the Scheduler
Kill by name:
pkill -f run_schedule.shOr 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.logFinal 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.