[Tutorial] systemd timer that can send a notification:

This has taken me nearly a day and a lot of reading to get it to work!

Scenario - I wanted a notification every n minutes of any updates available, but this is easily adapted to other uses, just change the script.

  1. The script (uses auracle & checkupdates):
#!/usr/bin/env bash

# Run by /etc/systemd/system/archup.service + timer

export DBUS_SESSION_BUS_ADDRESS="unix:path=/run/user/$UID/bus"

echo $(date) > ~/.avail.txt

/usr/bin/checkupdates >> ~/.avail.txt

echo "-----------------------------------------------" >> ~/.avail.txt

/usr/bin/auracle outdated >> ~/.avail.txt

notify-send "$(cat ~/.avail.txt)"

echo $(date) >> ~/test 

The export DBUS_SESSION… is the important line, if it is missing, the following error occurs:

Cannot autolaunch D-Bus without X11 $DISPLAY
  1. The service:
# /etc/systemd/system/archup.service
[Unit]
Description=Service to run update notifier:

[Service]
Type=oneshot
ExecStart=/usr/bin/uup
User=xircon

The important line here is User=<your-user>. /usr/bin/uup is a symlink to the script.

  1. The Timer:
# /etc/systemd/system/archup.timer
[Unit]
Description=Logs some system statistics to the systemd journal

[Timer]

# Explained https://silentlad.com/systemd-timers-oncalendar-(cron)-format-explained

# Testing - Every 1 minute:
#OnCalendar=*:1/1

# Deployment - Every 30 minutes:
OnCalendar=*:0/30:00

Unit=archup.service

[Install]
WantedBy=multi-utimers.target
  1. Start the timer:
sudo systemctl enable archup.timer --now

A notification can be forced with:

sudo systemctl restart archup.timer

6 Likes

Am now working on a gmail script, will post it when I get it working…

why not simply use the eos-update-notifier package from the repo?

If you want more functions in there, you might be able to contribute to that one as it is EndeavourOS maintained.

Because I am trying to learn eww and needed something to integrate with it.

What is that?

:face_vomiting: :sweat_smile:

Learning is always a good thing. But I don’t understand why you’re adding in extra hoops here. As mentioned above, the eos-update-notifier script will generate a notification for the current user…because the systemd service the timer calls is run as a user service, installed in $HOME/.config/systemd/user. If you look at the service, you’ll that it only needs to add the DISPLAY=:0 variable to the environment before running:

# /home/ajgringo619/.config/systemd/user/eos-update-notifier.service
[Unit]
Description=EOS update notifier service

[Service]
Type=oneshot
Environment=DISPLAY=:0
ExecStart=/usr/bin/eos-update-notifier -systemd

[Install]
WantedBy=eos-update-notifier.timer

IMHO, a better project would be to learn about sending system notifications to logged in users.

When learning an instrument, we are taught to play the same notes as someone else. When we do it well enough, and like it, we experiment and make our own music.

In other words, we learn to recreate something from scratch that has already been made so we can create something entirely new. :thinking: