通知发送在 systemd 服务中不起作用

通知发送在 systemd 服务中不起作用
#!/bin/bash
/usr/bin/notify-send 'lol'
#function check_state () {
    #toggle_toggle=$(/usr/bin/amixer get Capture | grep -c "\[off\]")
    #echo toggle+percent
    #toggle_percent=$(/usr/bin/amixer get Capture | grep -c "\[0%\]")
    #echo toggle_percent
    #let toggle_result=$((toggle_toggle+toggle_percent))
    #echo $((toggle_result))
#}
#check_state 
#echo $toggle_result # IF 4  = TURNED OFF, IF !=4 TURNED ON

#if [ $((toggle_result)) != 4 ] ; then
        # toggle off
#        /usr/bin/killall -9 xfce4-notifyd
#        /usr/bin/notify-send 'Microphone was left turned on' 'It was automatically disabled.'
#   /usr/bin/amixer set Capture toggle
#        /usr/bin/amixer set Capture 0
#        /usr/bin/amixer set Capture 0%
#fi

exit 0

服务文件

[Unit]
Description=Warns if microphone is turned on at startup
After=sound.target
After=display-manager.service
[Service]
ExecStart=/opt/bin/microphone-status
RemainAfterExit=yes
Environment="DISPLAY=:0.0" "XAUTHORITY=/home/tom/.Xauthority"

[Install]
WantedBy=multi-user.target

请注意,当我运行 systemctl start microphone-status.service 时,它​​可以工作。但在启动时,却不行。我不明白原因

它成功了:

sudo systemctl status microphone-status.service 
[sudo] password for tom: 
● microphone-status.service - Warns if microphone is turned on at startup
     Loaded: loaded (/opt/bin/microphone-status.service; enabled; vendor preset: enabled)
     Active: active (exited) since Tue 2020-12-22 20:53:08 CET; 9min ago
    Process: 759 ExecStart=/opt/bin/microphone-status (code=exited, status=0/SUCCESS)
   Main PID: 759 (code=exited, status=0/SUCCESS)

Dec 22 20:53:08 UNKNOWN-PC systemd[1]: Started Warns if microphone is turned on at startup.

编辑1:

[Unit]
Description=Turns off microphone at startup
#After=multi-user.target
After=syslog.target

[Service]
Type=simple
ExecStart=/opt/bin/microphone-status
User=tom
RemainAfterExit=yes
Environment="DISPLAY=:0.0" "XAUTHORITY=/home/tom/.Xauthority"

[Install]
WantedBy=multi-user.target

相关内容