Send notification from systemd
At the end of a script I wrote, I want to send a notification to know when it ends. The content of the script is not important except the notification part.
Here is the important part of the script:
#!/bin/bash USER=<username> USERID=`id -u $USER` sudo -u $USER bash -c "DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/$USERID/bus notify-send -t 5000 -u normal -i /usr/share/icons/Adwaita/32x32/devices/drive-removable-media.png 'Ah! the element of surprise'"
When I run it from my terminal, it works well.
I've created a service file in /etc/systemd/system
with the following content:
[Unit] Description=Test notification Requires=home.mount After=home.mount [Service] ExecStart=/home/alexis/Personnalisation/Scripts/test.notification.sh Type=oneshot [Install] WantedBy=graphical.target
When I run it through sudo systemctl start test.notification
, it works well.
The problem arise when systemd runs after I run systemd enable test.notification
.
If I add other things in the script, they are done.
Is my service description wrong? Is my notification instruction missing something?
The problem is that systemd runs with a minimal environment and not all envvar are known during the script execution. To make it work, I've change bash
by /bin/bash
.
I've found out what was wrong by running the script without the environment:
env -i /path/to/script
It returned the following error:
sudo: bash: command not found
This error is self explanatory and helped me find the problem.