Creating a simple laptop notification
Path
The final step is to set up the systemd units that will monitor and trigger the execution of powerchange.sh
.
You use path
units to monitor files and directories. As you will be using upowerlog.sh
to update the upower.log
file, upower.log
is what you want to monitor. The path
unit shown in Listing 4 does that.
Listing 4
powerchange.path
01 [Unit] 02 Description= monitoring upower log 03 Wants= upowerlog.service 04 05 [Path] 06 PathModified= /home/[username]/.local/share/upower.log
We already talked about the Description
directive when explaining upowerlog.service
and the PathModified
directive is self-explanatory: It points to a file or a directory and is triggered if said file or directory is modified.
There is another new directive in powerchange.path
worth looking at in a little more depth, though. Line 3 shows a Wants
directive that points to the first unit you created, upowerlog.service
. This ensures that if upowerlog.service
is not started when you run powerchange.path
, systemd will get it started. This means you don't have to run upowerlog.service
yourself; systemd will do it for you the moment it parses the Wants= upowerlog.service
line.
You will notice that there is no call to powerchange.sh
, the script we created in Listing 3. That's because path units do not execute scripts themselves. Instead they rely on a companion service
script (usually with the same name, but with the .service
extension) to do that for them. After all, that is what service
units are for: starting scripts or programs.
powerchange.service
looks like what is shown in Listing 5. Again, this service is self-explanatory and all it does is run the powerchange.sh
script from Listing 3.
Listing 5
powerchange.service
01 [Service] 02 Type= simple 03 ExecStart= /home/[username]/.local/bin/powerchange.sh
Save both powerchange.path
and powerchange.service
to /home/[username]/.config/systemd/user/
, and you can now run powerchange.path
with
systemctl --user start powerchange.path
and check that everything works by unplugging your laptop. The notification shown in Figure 6 should pop up, warning you that your machine has been disconnected.
Walkthrough
To summarize what's going on:
- 1 When you start
powerchange.path
, systemd also startsupowerlog.service
thanks to theWants
directive inpowerchange.path
. - 2
upowerlog.service
runsupowerlog.sh
, which starts dumping the output ofupower -m
into/home/[username]/.local/share/upower.log
. - 3 Each time
/home/[username]/.local/share/upower.log
changes,powerchange.path
callspowerchange.service
. - 4
powerchange.service
runs thepowerchange.sh
script that checks to see if the line power source is connected. If it is not, it sends a notification to the desktop.
Conclusions
I hope this has helped illustrate how you can leverage a wide variety of seemingly unrelated tools to create one single project.
In any case, systemd units, the UPower system, notify-send
(and other command-line-to-desktop utilities), and Bash's daisy-chaining of terminal commands are all worth exploring further. They provide you with a rich set of tools you can use to complete all kinds of projects.
« Previous 1 2
Buy this article as PDF
(incl. VAT)
Buy Linux Magazine
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters
Support Our Work
Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.
News
-
Canonical Bumps LTS Support to 12 years
If you're worried that your Ubuntu LTS release won't be supported long enough to last, Canonical has a surprise for you in the form of 12 years of security coverage.
-
Fedora 40 Beta Released Soon
With the official release of Fedora 40 coming in April, it's almost time to download the beta and see what's new.
-
New Pentesting Distribution to Compete with Kali Linux
SnoopGod is now available for your testing needs
-
Juno Computers Launches Another Linux Laptop
If you're looking for a powerhouse laptop that runs Ubuntu, the Juno Computers Neptune 17 v6 should be on your radar.
-
ZorinOS 17.1 Released, Includes Improved Windows App Support
If you need or desire to run Windows applications on Linux, there's one distribution intent on making that easier for you and its new release further improves that feature.
-
Linux Market Share Surpasses 4% for the First Time
Look out Windows and macOS, Linux is on the rise and has even topped ChromeOS to become the fourth most widely used OS around the globe.
-
KDE’s Plasma 6 Officially Available
KDE’s Plasma 6.0 "Megarelease" has happened, and it's brimming with new features, polish, and performance.
-
Latest Version of Tails Unleashed
Tails 6.0 is based on Debian 12 and includes GNOME 43.
-
KDE Announces New Slimbook V with Plenty of Power and KDE’s Plasma 6
If you're a fan of KDE Plasma, you'll be thrilled to hear they've announced a new Slimbook with an AMD CPU and the latest version of KDE Plasma desktop.
-
Monthly Sponsorship Includes Early Access to elementary OS 8
If you want to get a glimpse of what's in the pipeline for elementary OS 8, just set up a monthly sponsorship to help fund its continued existence.