This folder contains 7 standalone Python scripts, each automating a single, specific task-scheduling need (in-process scheduling, cron, Windows Task Scheduler, periodic execution, one-off timed execution, retry-on-failure, and a background job queue).
Every script:
- Is completely independent — you only need the one file for the task you want.
- Can be run directly from the command line.
- Uses
argparse, so running it with-hshows help and usage. - Prints clear success/error messages so you always know what happened.
Repository: https://github.com/aw-junaid/Python-System-Administration/tree/main/modules/Task%20Scheduling/scripts
schedule_with_library.py,execute_periodically.py, andexecute_at_specific_time.pyonly run while the script itself is running. Close the terminal or lose the process and the schedule stops. For jobs that must survive reboots/logouts, useschedule_with_cron.py(Linux/macOS) orwindows_task_scheduler.py(Windows) instead — those register with the OS.schedule_with_cron.pymodifies your real crontab (the current user's). Runcrontab -lbefore and after to see what changed, and always give jobs a--commentlabel so you can find and remove them later with theremoveaction.windows_task_scheduler.pymodifies real Windows Task Scheduler entries and may require running your terminal as Administrator for some schedule types or system-level tasks.- Commands you schedule run with the same permissions as the user/script that scheduled them. Don't schedule commands from untrusted sources.
queue_background_jobs.py's worker actually executes every command in the queue — only add commands you trust, and checkjob_queue.json/job_queue.logif something unexpected runs.- Always test your command manually first (run it directly in the terminal) before scheduling it, so you know it works and know what "success" (exit code 0) looks like.
- Long-running loops (
execute_periodically.pywithout--count,schedule_with_library.py,queue_background_jobs.py workerwithout--once) run until you pressCtrl+C— know how to stop them.
- Python 3.9+ installed on your system.
- Check your version:
python3 --version
- Some scripts use only the Python standard library — nothing to install.
- Others need small external packages, listed in
requirements.txt. schedule_with_cron.pyrequires a workingcron/crontabinstallation and is Linux/macOS only.windows_task_scheduler.pywraps the built-inschtaskscommand and is Windows only.
-
Clone the repository:
git clone https://github.com/aw-junaid/Python-System-Administration.git cd "Python-System-Administration/modules/Task Scheduling/scripts"
-
(Recommended) Create a virtual environment:
python3 -m venv venv source venv/bin/activate # On Windows: venv\Scripts\activate
-
Install dependencies:
pip install -r requirements.txt
windows_task_scheduler.py,execute_periodically.py,execute_at_specific_time.py,retry_failed_task.py, andqueue_background_jobs.pywork with zero installs.schedule_with_library.pyneedsschedule;schedule_with_cron.pyneedspython-crontabandcroniter. -
Run any script:
python3 script_name.py --help
This shows the exact arguments each script expects.
Below, python3 is used for Linux/macOS. On Windows, use python instead.
Requires: pip install -r requirements.txt
python3 schedule_with_library.py "echo Hello" --every 10 --unit seconds
python3 schedule_with_library.py "python3 backup.py" --every 1 --unit hoursExpected output: Confirms the schedule, then runs the command every N --unit (seconds/minutes/hours/days), printing a timestamp and output each time it fires. Runs until Ctrl+C — the script itself must stay running.
Requires: pip install -r requirements.txt · Platform: Linux/macOS only
python3 schedule_with_cron.py add "0 2 * * *" "python3 /home/user/backup.py" --comment "daily-backup"
python3 schedule_with_cron.py list
python3 schedule_with_cron.py remove --comment "daily-backup"Expected output:
add: confirms the job was added to your crontab and shows its next run time.list: shows every cron job for your user, with enabled/disabled status.remove: confirms how many jobs with that comment/label were deleted.
Verify directly with the standard crontab -l command at any time.
Platform: Windows only
python windows_task_scheduler.py add "DailyBackup" "C:\Python312\python.exe C:\scripts\backup.py" --schedule DAILY --time 02:00
python windows_task_scheduler.py list
python windows_task_scheduler.py remove "DailyBackup"Expected output:
add:Task 'DailyBackup' created successfully.(usesschtasks /Createunder the hood).list: full output ofschtasks /Query, showing all scheduled tasks on the system.remove:Task 'DailyBackup' removed successfully.
Caution: Some schedule types or system-wide tasks require running your terminal as Administrator.
python3 execute_periodically.py "echo Hello" --interval 5
python3 execute_periodically.py "python3 healthcheck.py" --interval 60 --count 10Expected output: Runs the command every --interval seconds, printing a timestamp and output each time. With --count, stops automatically after that many runs; without it, runs forever until Ctrl+C.
python3 execute_at_specific_time.py "python3 backup.py" --time 23:30
python3 execute_at_specific_time.py "python3 report.py" --datetime "2026-08-01 09:00:00"Expected output: Prints the target time and how long it will wait, then blocks until that moment and runs the command once. --time HH:MM schedules for today (or tomorrow if that time already passed); --datetime targets an exact date and time. Ctrl+C cancels before it fires.
python3 retry_failed_task.py "curl -f https://example.com/api/ping"
python3 retry_failed_task.py "python3 upload.py" --max-retries 8 --base-delay 3Expected output: Runs the command; if it exits non-zero, retries with exponential backoff (delay doubles each attempt, capped at --max-delay, default 60s) up to --max-retries times (default 5). Prints Success on attempt N and exits 0 as soon as it succeeds, or gives up and exits 1 after the final attempt.
python3 queue_background_jobs.py add "python3 convert.py file1.csv"
python3 queue_background_jobs.py add "python3 convert.py file2.csv"
python3 queue_background_jobs.py list
python3 queue_background_jobs.py workerExpected output:
add:Job #N added to queue: <command>, stored injob_queue.json.list: every job with its status (pending,completed,failed).worker: processes pending jobs in order, one at a time, logging each start/output/result tojob_queue.log. Useworker --onceto drain the current queue and exit; without--once, it keeps polling for new jobs every--intervalseconds (default 5) untilCtrl+C.
To safely try these scripts without touching real schedules or system state:
mkdir test_area && cd test_area
python3 ../execute_periodically.py "echo tick" --interval 2 --count 3
python3 ../retry_failed_task.py "python3 -c \"import sys; sys.exit(0)\"" --max-retries 2
python3 ../queue_background_jobs.py add "echo queued job"
python3 ../queue_background_jobs.py worker --once- These scripts are provided for educational and administrative automation purposes.
- Always review a script's code before scheduling it to run automatically or unattended.
- The author is not responsible for issues caused by scheduled commands, including unintended crontab/Task Scheduler changes, runaway loops, or commands run with incorrect permissions.