Free tech learning for everyone, from basics to pro

ScriptingProgramming and Application SecurityBeginner

Scheduling scripts: cron, systemd timers and Windows Task Scheduler

In short: Scheduled scripts turn one-off checks into routine automation. On Linux use cron or systemd timers; on Windows use Task Scheduler. Always use full paths, run as a least-privilege account, capture output to a log and test the job before relying on it.

Why schedule scripts

A backup script that you must remember to run is not really automation. Scheduling lets the operating system start your script at fixed times, at boot or when an event happens. Typical jobs include nightly configuration backups, hourly reachability checks, weekly certificate expiry reports and log clean-up.

cron vs Task Schedulercron (Linux)One line per jobcrontab -e to editFive time fieldsMinimal environmentLogs via syslog or journalTask Scheduler (Windows)Trigger, action, settingsGUI or PowerShellTime, logon, event triggersRuns as chosen accountHistory in event log
Figure 1. Both run scripts unattended; cron is a one-line text format, Task Scheduler uses structured triggers and actions.

cron on Linux

The five cron fieldsMinute0-59Hour0-23Day of month1-31Month1-12Day of week0-7, Sun = 0 or 7Commandfull paths
Figure 2. A crontab line is five time fields followed by the command to run.

Edit your own schedule with crontab -e and list it with crontab -l. Each line has five time fields and a command.

# m  h  dom mon dow  command
  15 2  *   *   *    /opt/netops/backup_configs.sh >> /var/log/netops/backup.log 2>&1
  */10 * *  *   *    /opt/netops/reach_check.sh /opt/netops/core.txt
  0  7  *   *   1    /usr/bin/python3 /opt/netops/cert_report.py

The first job runs at 02:15 every day and appends both output and errors to a log. The second runs every ten minutes. The third runs at 07:00 every Monday. If you are unsure of a time expression, write it in a comment in plain English next to the line.

cron runs jobs with a very small environment: a short PATH, no aliases and no interactive profile. That is the number one reason a script works by hand and fails under cron. Use full paths to commands and files, or set PATH at the top of the crontab.

systemd timers

On modern distributions a systemd timer is a good alternative. It is two small files: a .service unit that runs the script and a .timer unit that says when. Output goes to the journal automatically, and Persistent=true runs a missed job after the machine was switched off.

# /etc/systemd/system/netbackup.timer
[Unit]
Description=Nightly network config backup

[Timer]
OnCalendar=*-*-* 02:15:00
Persistent=true

[Install]
WantedBy=timers.target

Enable it with systemctl enable --now netbackup.timer, check it with systemctl list-timers and read output with journalctl -u netbackup.service.

Windows Task Scheduler

A scheduled task has a trigger (when), an action (what to run) and a principal (which account). You can build it in the GUI or with PowerShell, which is easier to repeat across servers.

$action  = New-ScheduledTaskAction -Execute 'pwsh.exe' `
           -Argument '-NoProfile -File "C:\Scripts\Check-Servers.ps1"'
$trigger = New-ScheduledTaskTrigger -Daily -At 6:45am
Register-ScheduledTask -TaskName 'NetOps-DailyCheck' -Action $action `
    -Trigger $trigger -User 'NT AUTHORITY\SYSTEM' -RunLevel Highest

For tasks that need network access to other servers, use a group Managed Service Account (gMSA) or a dedicated low-privilege service account instead of a personal login, so the task does not break when someone changes their password or leaves.

  1. Run the script by hand as the same account the scheduler will use.
  2. Use full paths and write output to a log file.
  3. Make the script exit non-zero on failure and alert on it.
  4. Check the first few scheduled runs in the log or task history.

Tip: stagger heavy jobs (for example 02:15 instead of 02:00) so dozens of jobs do not all hit the network and storage at the same minute.

Practice

What does the cron line "0 7 * * 1" mean?
Run at 07:00 every Monday.
Why does a script often work by hand but fail in cron?
cron uses a minimal environment with a short PATH and no profile, so commands or relative paths the script relies on are not found.
Why use a service account or gMSA for a scheduled task?
It gives the task its own least-privilege identity that does not depend on a person, so it keeps working when staff change and is easier to audit.

Key takeaways

  • cron uses five time fields followed by a command.
  • Use full paths and redirect output to a log.
  • systemd timers add journal logging and catch-up runs.
  • Task Scheduler jobs have a trigger, an action and a run-as account.
  • Run scheduled jobs under dedicated least-privilege accounts.
Tracks your learning on this device only. See your progress on the learning paths.

About the author

Mohamed Fayas

Founder of FM TecHub. CCNA certified, with a BTEC HND in Network Engineering, and BEng (Hons) in Computer Networking and Cloud Security. He writes practical, plain-English guides on networking, security and cloud.

More about the author

Original content written by Mohamed Fayas for FM TecHub. © 2026 FM TecHub. You're welcome to share a link to this page; please don't copy the text elsewhere without permission.

Stay up to date by email

New notes, tools and security news from FM TecHub, explained simply. Free.

We only use your email to send these updates. See our privacy policy.