Outils pour utilisateurs

Outils du site


pele_mele:stack_exchange:superuser:1225022

Différences

Ci-dessous, les différences entre deux révisions de la page.

Lien vers cette vue comparative

Les deux révisions précédentesRévision précédente
pele_mele:stack_exchange:superuser:1225022 [2024/11/23 03:55] – supprimée - modification externe (Date inconnue) 127.0.0.1pele_mele:stack_exchange:superuser:1225022 [2024/11/23 03:55] (Version actuelle) – ↷ Page déplacée et renommée de pele_mele:stack_exchange:superuser-1225022 à pele_mele:stack_exchange:superuser:1225022 alexis
Ligne 1: Ligne 1:
 +====== 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:
 +
 +<code bash>
 +#!/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'"
 +</code>
 +When I run it from my terminal, it works well.
 +
 +I've created a service file in ''/etc/systemd/system'' with the following content:
 +<code>
 +[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
 +</code>
 +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?
 +
 +<WRAP help>
 +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:
 +
 +<code>
 +env -i /path/to/script
 +</code>
 +It returned the following error:
 +
 +> sudo: bash: command not found
 +
 +This error is self explanatory and helped me find the problem.
 +</WRAP>
 +<WRAP info>
 +[[https://superuser.com/questions/1225022/send-notification-from-systemd|Send notification from systemd - Super User]]
 +</WRAP>