Skip to content

Instantly share code, notes, and snippets.

@braindevices
Last active December 20, 2023 17:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save braindevices/c5c0d0664af43164729296ca8e5a7f21 to your computer and use it in GitHub Desktop.
Save braindevices/c5c0d0664af43164729296ca8e5a7f21 to your computer and use it in GitHub Desktop.
strange systemd timer bug: restart the timer trigger the unit run
#!/bin/bash
set -x
systemctl -T -o verbose daemon-reload
systemctl -T -o verbose disable --now test-timer.timer
set -e
wait_secs=20
run_at_time=$(date -d "+${wait_secs}sec" +"%H:%M:%S")
logfile=/tmp/test-timer-output
if [ -f ${logfile} ]
then
rm ${logfile}
fi
cat > /etc/systemd/system/test-timer.service <<EOL
[Unit]
Description=Run test
[Service]
ExecStart=/bin/bash -c "date >> ${logfile}"
EOL
cat > /etc/systemd/system/test-timer.timer <<EOL
[Unit]
Description=A test timer at ${run_at_time}
[Timer]
OnCalendar=*-*-* ${run_at_time}
Unit=test-timer.service
[Install]
WantedBy=timers.target
EOL
systemctl -T -o verbose daemon-reload
systemctl -T -o verbose enable --now test-timer.timer
set +e
echo "expect run at ${run_at_time}"
echo "now is $(date), wait ${wait_secs}secs"
sleep ${wait_secs}
echo "now is $(date)"
systemctl status test-timer.timer
systemctl status test-timer.service
cat ${logfile}
# touch /etc/systemd/system/test-timer.service
# touch /etc/systemd/system/test-timer.timer
sleep 10
echo "now is $(date)"
systemctl -T -o verbose daemon-reload
systemctl -T -o verbose stop test-timer.timer
systemctl -T -o verbose start test-timer.timer
sleep 1
systemctl status --no-pager test-timer.timer
systemctl status --no-pager test-timer.service
cat ${logfile}
# touch /etc/systemd/system/test-timer.service
# touch /etc/systemd/system/test-timer.timer
sleep 10
echo "now is $(date)"
systemctl -T -o verbose daemon-reload
systemctl -T -o verbose stop test-timer.timer
systemctl -T -o verbose start test-timer.timer
sleep 1
systemctl status --no-pager test-timer.timer
systemctl status --no-pager test-timer.service
cat ${logfile}
@braindevices
Copy link
Author

run the test script we can see that we will end up 2 lines in /tmp/test-timer-output, not 1 or 3

@braindevices
Copy link
Author

braindevices commented Dec 17, 2023

This is maybe rather redhat problem instead of systemd problem (on RHEL8, AlmaLinux8 and AlmaLinux9), I cannot reproduce it on any ubuntu versions.

different flags:
in redhat not in ubuntu
{'+P11KIT',
'+TPM2',
'+XKBCOMMON',
'-APPARMOR',
'-BPF_FRAMEWORK',
'-FIDO2',
'-IPTC'}
in ubuntu not in redhat
{'+APPARMOR', '+FIDO2', '+IPTC', '-P11KIT', '-XKBCOMMON'}

@braindevices
Copy link
Author

the touch is not necessary, cat the log file is not very reliable way to find if the 2nd line has been appended, thus I sleep 1 sec before cat. This shows 2 lines in the file without touch the timer.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment