0

I would like to run a script every 30 minutes on my RbPi, but not immediately after boot.
Example: If I boot my RbPi at 15:15, the script should run at 15:30 not before.
Setup: My RbPi has no internet connection to sync its time, it relies on an external RTC. To sync the time I followed this post.

What I did so far:
I have the following two files, generated using
sudo systemctl edit --force --full test.service and
sudo systemctl edit --force --full test.timer

# test.service
[Unit]
Description=test

[Service]
Type=simple
ExecStart=echo "TEST"

# test.timer
[Unit]
Description=test

[Timer]
OnCalendar=*:0/30
Persistent=false

[Install]
WantedBy=default.target

Unfortunately this dummy script runs immediately during the boot sequence as shown by journalctl -r:

...

Oct 21 14:11:43 rbpi0 kernel: Bluetooth: BNEP (Ethernet Emulation) ver 1.3
Oct 21 14:11:43 rbpi0 systemd[1]: Finished Online ext4 Metadata Check for All Filesystems.
Oct 21 14:11:43 rbpi0 systemd[1]: e2scrub_all.service: Succeeded.
Oct 21 14:11:43 rbpi0 systemd[1]: Starting Daily man-db regeneration...
Oct 21 14:11:43 rbpi0 systemd-udevd[128]: Using default interface naming scheme 'v247'.
Oct 21 14:11:43 rbpi0 bluetoothd[443]: Starting SDP server
Oct 21 14:11:43 rbpi0 systemd[1]: Starting Rotate log files...
Oct 21 14:11:43 rbpi0 bluetoothd[443]: Bluetooth daemon 5.55
Oct 21 14:11:43 rbpi0 echo[455]: TEST
Oct 21 14:11:43 rbpi0 systemd[1]: Starting test...
Oct 21 14:11:42 rbpi0 systemd[1]: Starting Online ext4 Metadata Check for All Filesystems...
Oct 21 14:11:42 rbpi0 kernel: rtc-rv3028 1-0052: setting system clock to 2023-10-21T14:11:42 UTC (1697897502)

How can I avoid my script to run during the boot sequence but run only at *:30?

1

4 Answers 4

0

Systemd scripts are run asynchronously as root before boot and is not suitable.

Linux has a tool cron to run tasks at a given time.

It can run after boot, yearly, monthly, weekly, daily, hourly etc. and can run as root or as a user.

See man 5 crontab

I use to turn the LED on my Pi on/off overnight and it is used by the OS to perform tasks e.g. monthly log rotation.

2
  • Thank you for your answer. I know how to use cron but this is not what I wanted to do. I will try to investigate if running my script as user will fix the issue.
    – Pa Dalton
    Oct 22, 2023 at 8:14
  • In the end I changed to your solution: it is simply more reliable :) .
    – Pa Dalton
    Dec 13, 2023 at 16:18
0

I'd probably modify the script to check how long since boot. Something like this (Bash syntax):

up_sec=$(sed 's/\..*//' /proc/uptime)
if ! (( up_sec > 15 * 60 )); then
    echo "Not up long enough yet ($up_sec), skipping"
    exit 0
fi
2
  • Does this mean it is expected to run at boot?
    – Pa Dalton
    Oct 21, 2023 at 20:47
  • It'd just run whenever it would run according to the OP. The difference is when the script runs if it detects the host has been up on briefly then it would skip doing anything
    – Waxrat
    Oct 22, 2023 at 23:50
0

The Linux at command allows you to run something at a particular time. The time can be specified in different ways.

For the example in the question, run this command at boot-up:

at -f /path/scriptname now + 15 minutes

See the man page for further details.

1
  • Thank you for your answer. That is not exactly what I wanted to do. I really need the script to be executed at 00 and 30, not just every 30 minutes. I'm looking for a solution that resolves the issue with systemd.
    – Pa Dalton
    Nov 16, 2023 at 10:44
0

Eureka!

The issue was that i dindn't initialized the time correctly. Yes I followed this post but I used a script that wrapped the /sbin/hwclock --hctosys --utc --noadjfile command, delaying it in time. This let the boot sequence to start already creating a mess with the OnCalendar= entry (as described in the man page).

I was enough to add a service exactly as this, no need to change anything else.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.