0

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?

2
  • Perhaps it ends before you've logged in? Jul 2, 2017 at 17:36
  • for that simple example, that might be the case. But for the script I want to monitor, it waits until a drive is mounted. This drive is mounted manually.
    – A.D.
    Jul 2, 2017 at 19:02

1 Answer 1

1

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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .