Debian 13 Tutorial / Schedule systemd Timer Jobs and Diagnose Failures
In Debian 13, systemd timers set the execution time, and the linked service performs the actual work. Even if a timer is active, the task may fail, so you should check not only the next schedule but also the service exit code, logs, and generated results.
Check the File-copy Lab’s Requirements
This time, a single text file will be copied to a result file from the dedicated folder of the regular user lfstudent. If the original and result contents are the same, it will not overwrite, and if another execution is happening simultaneously, it will be rejected by a lock. This is not a backup program that preserves data from multiple points in time, but rather targets small test files where the original does not change during the operation.
We use flock, mktemp, and cmp on systemd 257 of Debian 13. flock is a command that obtains an exclusive lock between processes based on a file. If new units, folders, or scripts already exist, check the contents first and do not overwrite.
id lfstudent
command -v flock mktemp cmp
systemctl show lf-timer18.timer -p LoadState
ls -ld /home/lfstudent/lf-timer18
ls -l /usr/local/libexec/lf-timer18.sh /etc/systemd/system/lf-timer18.service /etc/systemd/system/lf-timer18.timer
Confirm that the timer is not-found and that the remaining practice paths do not exist. File not found errors are an expected result of checking in advance. If the tools do not exist, check the packages like apt-cache show util-linux coreutils diffutils to install the required tools. Runtime state and startup configuration use the distinction covered in systemd service and drop-in lab .
Write an Idempotent File-copy Script
Log in with lfstudent to create a dedicated original. If the same folder exists, the command below will not overwrite it.
mkdir /home/lfstudent/lf-timer18 &&
printf 'scheduled-copy-v1\n' > /home/lfstudent/lf-timer18/source.txt
Prepare a folder to save the script with administrator privileges and write the entire content below from sudo nano /usr/local/libexec/lf-timer18.sh. If nano is not available, choose an editor you are using.
sudo install -d /usr/local/libexec
#!/bin/sh
set -eu
task_dir=/home/lfstudent/lf-timer18
exec 9>"$task_dir/.lock"
if ! flock -n 9; then
printf 'Another copy is running\n' >&2
exit 75
fi
test -r "$task_dir/source.txt"
if test -f "$task_dir/snapshot.txt" && cmp -s "$task_dir/source.txt" "$task_dir/snapshot.txt"; then
printf 'Snapshot unchanged\n'
exit 0
fi
task_tmp=$(mktemp "$task_dir/.snapshot.XXXXXX")
trap 'rm -f -- "$task_tmp"' EXIT HUP INT TERM
cat "$task_dir/source.txt" > "$task_tmp"
mv -- "$task_tmp" "$task_dir/snapshot.txt"
printf 'Snapshot updated\n'
This is a script to be saved in a file and is not executed as a command by pasting it into the terminal to replace the entire current shell. After saving, check the execution permission and syntax.
sudo chmod 755 /usr/local/libexec/lf-timer18.sh
sh -n /usr/local/libexec/lf-timer18.sh
ls -l /usr/local/libexec/lf-timer18.sh
Executable files owned by administrators cannot be modified by regular users, and the actual data folder is used by lfstudent. mktemp creates a temporary file in the same folder and renames it after copying, so during normal replacement, the reader does not read a partially written result path. This example does not guarantee durability after disk failure or a consistent copy of a database being modified concurrently. The new result file inherits the restricted access permissions of mktemp.
Idempotency means that repeating an operation with the same input leaves the same result. Here, copying the same input again does not append duplicate data. This script outputs Snapshot unchanged and exits if the content is the same. Locking only works between cooperative executions using the same file. It does not prevent other programs from modifying the original. You can check non-blocking locks and exit behavior in flock manual .
Create the oneshot Service and Interval Timer
Save the work unit as sudo nano /etc/systemd/system/lf-timer18.service.
[Unit]
Description=Copy one lesson 18 test file
[Service]
Type=oneshot
User=lfstudent
ExecStart=/usr/local/libexec/lf-timer18.sh
Since this is a short-running task that ends quickly, we use oneshot. Because it needs to be called repeatedly, we do not keep it active with RemainAfterExit=yes. This service will be triggered by a timer, so no separate [Install] connection is created.
Save the following in sudo nano /etc/systemd/system/lf-timer18.timer. The 5-second and 20-second intervals are practice intervals for observing results quickly and are not recommended for operational cycles.
[Unit]
Description=Lesson 18 interval timer
[Timer]
OnActiveSec=5s
OnUnitInactiveSec=20s
AccuracySec=1s
Unit=lf-timer18.service
[Install]
WantedBy=timers.target
After activating the timer, the first execution is requested at about 5 seconds, and it is configured to run again about 20 seconds after the work unit enters an inactive state. This does not guarantee precise real-time execution and may be delayed depending on load, accuracy settings, etc. If the same service is running, the timer will not execute a separate service instance simultaneously. Considering other entry points where the script may be run directly, a lock is also placed inside the script. Schedule options are based on the systemd.timer documentation.
sudo systemd-analyze verify /etc/systemd/system/lf-timer18.service /etc/systemd/system/lf-timer18.timer
sudo systemctl daemon-reload
sudo systemctl start lf-timer18.service
systemctl show lf-timer18.service -p Result -p ExecMainCode -p ExecMainStatus -p ActiveState
cmp /home/lfstudent/lf-timer18/source.txt /home/lfstudent/lf-timer18/snapshot.txt
sudo systemctl enable --now lf-timer18.timer
systemctl list-timers lf-timer18.timer --no-pager
Manual execution should report Result=success and exit status 0, and cmp must succeed without output. Then, turn on the timer. The inactive state of a completed oneshot service can be a normal result, so read both the Result and exit code together. The timer will display the next scheduled execution and the last execution time.
After the first scheduled activation, check the timer, job result and copied data.
systemctl is-active lf-timer18.timer
systemctl show lf-timer18.service -p Result -p ExecMainStatus -p ActiveState
sudo journalctl -u lf-timer18.service -b --no-pager -n 20
cat /home/lfstudent/lf-timer18/snapshot.txt
cmp /home/lfstudent/lf-timer18/source.txt /home/lfstudent/lf-timer18/snapshot.txt
Check journal timestamps together with Snapshot updated or Snapshot unchanged, and the actual results. In this exercise, we confirmed that the timer activates the service that runs the file-copy script, and when re-executing with the same source, it ends with no changes.
Compare Job Failure with an Active Timer
Without deleting the original, change only its name to create a read failure. First, make sure that source.saved does not already exist. Execute this only in a dedicated exercise with no other tasks using the same data folder.
test ! -e /home/lfstudent/lf-timer18/source.saved &&
mv /home/lfstudent/lf-timer18/source.txt /home/lfstudent/lf-timer18/source.saved
sudo systemctl start lf-timer18.service
systemctl is-active lf-timer18.timer
systemctl show lf-timer18.service -p Result -p ExecMainCode -p ExecMainStatus -p ActiveState
sudo journalctl -u lf-timer18.service -b --no-pager -n 12
systemctl list-timers lf-timer18.timer --no-pager
The start request fails, the task’s Result shows exit-code, and ExecMainStatus appears as 1. The timer may remain active, and the next schedule also remains. That is, the fact that the schedule is maintained does not mean the file copy was successful. In the exercise, it can be confirmed that after failure, the previous valid result file remains intact. If the original is not restored before the timer runs again, the same failure may repeat.
mv /home/lfstudent/lf-timer18/source.saved /home/lfstudent/lf-timer18/source.txt
sudo systemctl start lf-timer18.service
systemctl show lf-timer18.service -p Result -p ExecMainStatus
cmp /home/lfstudent/lf-timer18/source.txt /home/lfstudent/lf-timer18/snapshot.txt
After restoring the original, verify Result=success, exit status 0 and content match through manual re-execution. Simply executing reset-failed does not restore the original file. In operations, logs must be kept and task results monitored, or failure notifications configured separately.
Test Lock Contention and Retry After Release
To avoid mixing with automatic calls and comparison, first stop this exercise timer. Disabling also removes this timer’s startup link.
sudo systemctl disable --now lf-timer18.timer
In terminal A, logged in as lfstudent, hold the lock temporarily.
flock /home/lfstudent/lf-timer18/.lock sleep 20
Execute on Terminal B with the same user before 20 seconds pass.
/usr/local/libexec/lf-timer18.sh
echo $?
Another copy is running and the exit code 75 defined by the script are expected. This is not the default collision code of flock itself, but a value distinguished by the script. If you run it again after the command on Terminal A finishes, it will succeed. Do not determine that it is locked just because the lock file remains. The standard is whether the running process is holding the file, and if you delete the file to resolve a collision, a separate lock target may be created, allowing duplicate execution.
Choose Calendar Timers or cron
If you need calendar-based times, like 2 a.m. KST daily, instead of interval timers for practice, consider OnCalendar. First, parse the expression as below to check the expected time.
systemd-analyze calendar '*-*-* 02:00:00 Asia/Seoul'
Below is a separate example that can be used when replacing the existing timer file. Replace the interval settings; do not retain OnActiveSec and OnUnitInactiveSec alongside this calendar schedule. If multiple schedule conditions are used together, it can be triggered by any one of the conditions. Actual automatic execution and failure comparison in this lecture were performed using the short interval timer mentioned earlier.
[Unit]
Description=Daily lesson 18 file copy
[Timer]
OnCalendar=*-*-* 02:00:00 Asia/Seoul
Persistent=true
Unit=lf-timer18.service
[Install]
WantedBy=timers.target
If you choose the calendar variant, first stop this lab timer, replace its entire file and run sudo systemd-analyze verify /etc/systemd/system/lf-timer18.timer. If validation succeeds, run sudo systemctl daemon-reload, then sudo systemctl enable --now lf-timer18.timer and inspect systemctl list-timers lf-timer18.timer --no-pager. To return to the short interval test, stop the timer, restore the earlier complete file, validate, reload and start it again. Calendar parsing was checked in the original lab; missed-run catch-up was not exercised.
Persistent=true is used to call tasks to make up for missed executions when the calendar timer was inactive during that time. It does not sequentially reproduce all executions for the number of times missed, nor is it an option that has the same effect as the preceding interval timer. Calendar-based tasks also need to check synchronization with the system time and time zone. The expression rules can be found in the systemd.time documentation .
| Requirements | Judgment Criteria |
|---|---|
| Comprehensive management of service accounts, dependencies, exit codes, and journal | Review of systemd service and timer combinations |
| Maintain existing simple user-specific periodic commands | Check the existing cron setup, environment variables and output handling |
| Make up for calendar tasks during the time it was turned off | Check the timer’s Persistent setting; do not assume ordinary cron provides equivalent catch-up behavior |
| Prevent duplicate execution of the same script | For any scheduling tool, check other entry points and lock design as well |
As in the Debian crontab manual, cron requires separate consideration of the executing user, environment, and mail or output handling conditions. Do not register the same task redundantly in both cron and timer. More important than the choice of tool is a configuration where you can observe the actual execution results and failures.
Remove Only the Lab Schedule and Files
If you enabled the Persistent calendar variant, stop the timer and run sudo systemctl clean --what=state lf-timer18.timer before deleting its unit file to remove its stored activation timestamp. The short interval version does not create this Persistent state.
If you applied a calendar example, that timer is also subject to cleanup. Stop the schedule first, confirm that any running tasks have finished, and remove only the current file.
sudo systemctl disable --now lf-timer18.timer
sudo systemctl stop lf-timer18.service
sudo rm -- /etc/systemd/system/lf-timer18.timer /etc/systemd/system/lf-timer18.service /usr/local/libexec/lf-timer18.sh
sudo systemctl daemon-reload
rm -- /home/lfstudent/lf-timer18/source.txt /home/lfstudent/lf-timer18/snapshot.txt /home/lfstudent/lf-timer18/.lock
rmdir -- /home/lfstudent/lf-timer18
systemctl show lf-timer18.timer -p LoadState
systemctl show lf-timer18.service -p LoadState
Ensure that both units are not-found and that the folder has been deleted. If the original temporary name or other files remain in the folder, review the contents before processing, and do not use recursive deletion to bypass that check. Preserve shared directories such as /usr/local/libexec, other timers and existing journal files.




