Bird
Raised Fist0
Linux CLIscripting~10 mins

systemd timers in Linux CLI - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - systemd timers
Create .service file
↓
Create .timer file
↓
Enable timer with systemctl
↓
Timer triggers at set time
↓
systemd runs .service file
↓
Service executes task
↓
Timer waits for next trigger
This flow shows how systemd timers work: you create a service and a timer file, enable the timer, and systemd runs the service at scheduled times.
Execution Sample
Linux CLI
[Unit]
Description=Run my script

[Service]
Type=oneshot
ExecStart=/usr/bin/echo Hello from systemd timer

--- myjob.service ---

[Unit]
Description=Timer for myjob

[Timer]
OnBootSec=10s
Unit=myjob.service

[Install]
WantedBy=timers.target

--- myjob.timer ---
This example defines a service that echoes a message and a timer that triggers it 10 seconds after boot.
Execution Table
StepActionEvaluationResult
1Create myjob.service fileFile created with echo commandService ready to run
2Create myjob.timer fileTimer set to trigger 10 seconds after bootTimer ready to trigger service
3Enable timer with systemctlsystemctl enable myjob.timerTimer enabled and started
4System bootsTimer counts 10 secondsTimer triggers service
5systemd runs myjob.serviceExecStart runs echo commandOutput: Hello from systemd timer
6Timer waits for next triggerNo repeat set, timer stopsTimer inactive until next boot or manual start
💡 Timer stops because no repeat interval is set; service ran once after 10 seconds from boot
Variable Tracker
VariableStartAfter Step 3After Step 4After Step 5Final
Timer Stateinactiveactive (enabled)counting downtriggeredinactive (no repeat)
Service Statenot runningnot runningnot runningrunningcompleted
Key Moments - 3 Insights
Why doesn't the service run immediately after enabling the timer?
Because the timer is set with OnBootSec=10s, it waits 10 seconds after boot before triggering the service, as shown in execution_table step 4.
What happens if the timer has no repeat interval?
The timer triggers the service once and then becomes inactive, as seen in execution_table step 6.
How does systemd know which service to run when the timer triggers?
The timer file specifies the Unit=myjob.service, linking the timer to the service, as shown in the execution_sample code and step 2.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the Timer State after step 4?
Ainactive
Btriggered
Ccounting down
Dactive but not counting
💡 Hint
Check variable_tracker Timer State column after Step 4
At which step does the service actually run the echo command?
AStep 3
BStep 5
CStep 4
DStep 6
💡 Hint
Look at execution_table Action and Result columns for when ExecStart runs
If you want the timer to run the service every minute, what would change in the timer file?
AChange OnBootSec=10s to OnUnitActiveSec=1min
BChange Unit=myjob.service to Unit=myjob.timer
CRemove the [Timer] section
DAdd Type=oneshot to the timer file
💡 Hint
Refer to execution_sample and think about timer scheduling options
Concept Snapshot
systemd timers run tasks on schedule.
Create a .service file for the task.
Create a .timer file to schedule it.
Enable timer with systemctl.
Timer triggers service at set times.
Useful for automation without cron.
Full Transcript
Systemd timers automate running tasks by linking a timer file to a service file. First, you create a service file that defines what to run, like a script or command. Then, you create a timer file that tells systemd when to run that service, for example, 10 seconds after boot. You enable the timer with systemctl, which starts counting. When the timer reaches the set time, systemd runs the service. If no repeat is set, the timer stops after running once. This method is a modern alternative to cron for scheduling tasks.

Practice

(1/5)
1. What is the main purpose of a systemd timer in Linux?
easy
A. To manage user permissions
B. To schedule and run tasks automatically at specified times
C. To monitor system hardware status
D. To update software packages automatically

Solution

  1. Step 1: Understand systemd timer role

    Systemd timers are designed to schedule tasks to run automatically without manual intervention.
  2. Step 2: Compare with other options

    Monitoring hardware, managing permissions, and updating packages are not the primary roles of systemd timers.
  3. Final Answer:

    To schedule and run tasks automatically at specified times -> Option B
  4. Quick Check:

    Systemd timers = schedule tasks [OK]
Hint: Timers schedule tasks like alarms for programs [OK]
Common Mistakes:
  • Confusing timers with monitoring tools
  • Thinking timers manage user permissions
  • Assuming timers update software automatically
2. Which of the following is the correct syntax to enable a systemd timer named backup.timer?
easy
A. systemctl enable backup.timer
B. systemctl start backup.timer
C. systemctl run backup.timer
D. systemctl activate backup.timer

Solution

  1. Step 1: Identify enabling command for systemd timers

    The command systemctl enable sets a timer to start automatically on boot.
  2. Step 2: Differentiate from other commands

    start runs the timer immediately but does not enable it; run and activate are invalid commands.
  3. Final Answer:

    systemctl enable backup.timer -> Option A
  4. Quick Check:

    Enable timer = systemctl enable [OK]
Hint: Use 'enable' to start timer on boot, 'start' to run now [OK]
Common Mistakes:
  • Using 'start' instead of 'enable' to auto-run timers
  • Using invalid commands like 'run' or 'activate'
  • Confusing service commands with timer commands
3. Given this timer unit file snippet:
[Timer]
OnCalendar=*-*-* 12:00:00
Persistent=true

What does the Persistent=true setting do?
medium
A. Prevents the timer from running at boot
B. Runs the timer only once
C. Disables the timer after one run
D. Runs missed jobs immediately after boot

Solution

  1. Step 1: Understand Persistent=true meaning

    This setting makes systemd run any missed timer jobs immediately after the system boots.
  2. Step 2: Eliminate other options

    It does not limit runs to once, disable timers, or prevent running at boot.
  3. Final Answer:

    Runs missed jobs immediately after boot -> Option D
  4. Quick Check:

    Persistent=true = catch missed runs after reboot [OK]
Hint: Persistent=true runs missed tasks after reboot [OK]
Common Mistakes:
  • Thinking it runs only once
  • Assuming it disables timer after run
  • Believing it prevents timer at boot
4. You created a timer file cleanup.timer but it never triggers the service. Which is the most likely mistake?
medium
A. The timer file is not named exactly like the service file
B. The timer file is missing the [Timer] section
C. The service file cleanup.service is not enabled
D. The timer file uses OnBootSec= instead of OnCalendar=

Solution

  1. Step 1: Check timer and service file naming

    Systemd timers must have matching names with their service files except for the extension.
  2. Step 2: Analyze other options

    Missing [Timer] section causes syntax errors; service enabling is not required for timers to trigger; both OnBootSec and OnCalendar are valid.
  3. Final Answer:

    The timer file is not named exactly like the service file -> Option A
  4. Quick Check:

    Timer and service names must match [OK]
Hint: Timer and service filenames must match except extension [OK]
Common Mistakes:
  • Ignoring filename matching between timer and service
  • Assuming service must be enabled for timer to run
  • Confusing valid timer options as errors
5. You want a systemd timer to run a backup service every day at 3:30 AM but only if the system was off at that time. Which timer configuration achieves this?
hard
A. [Timer] OnActiveSec=03:30:00 Persistent=true
B. [Timer] OnCalendar=*-*-* 03:30:00 AccuracySec=1min
C. [Timer] OnCalendar=*-*-* 03:30:00 Persistent=true
D. [Timer] OnBootSec=03:30:00 Persistent=false

Solution

  1. Step 1: Identify daily schedule at 3:30 AM

    The OnCalendar=*-*-* 03:30:00 sets the timer to run daily at 3:30 AM.
  2. Step 2: Ensure missed runs execute after downtime

    Persistent=true makes systemd run missed jobs after system was off at scheduled time.
  3. Step 3: Evaluate other options

    AccuracySec adjusts timing precision but doesn't affect missed runs; OnBootSec and OnActiveSec run relative to boot or activation, not fixed time.
  4. Final Answer:

    [Timer] OnCalendar=*-*-* 03:30:00 Persistent=true -> Option C
  5. Quick Check:

    Daily 3:30 AM with catch-up = OnCalendar + Persistent [OK]
Hint: Use OnCalendar with Persistent=true for daily missed runs [OK]
Common Mistakes:
  • Using OnBootSec instead of OnCalendar for fixed time
  • Forgetting Persistent=true to run missed jobs
  • Confusing AccuracySec with scheduling behavior
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard