如何使用通知发送服务系统cd



我有一个服务,这个服务执行一些shell脚本

我想每15分钟运行一次这个脚本(现在是1分钟(

暂停.sh

#!/bin/bash
echo 'Hello world'
notify-send 'Please take a rest and come back again' &
sleep 10
systemctl suspend

暂停服务

[Unit]
Description=suspend service
[Service]
Type=simple
ExecStart=/home/<user>/suspending.sh
User=<user>
RemainAfterExit=yes
Environment="DISPLAY=:0" "XAUTHORITY=/home/<user>/.Xauthority"
[Install]
WantedBy=multi-user.target

暂停计时器

[Unit]
Description=Suspend the system
[Timer]
Unit=suspend.service
OnUnitActiveSec=1min

[Install]
WantedBy=graphical.target

sudo journalcl-f-u suspend.service

systemd[1]: Started suspend service.
suspending.sh[7578]: Hello world
suspending.sh[7585]: Failed to set wall message, ignoring: Interactive authentication required.
suspending.sh[7585]: Failed to suspend system via logind: Interactive authentication required.
suspending.sh[7585]: Failed to start suspend.target: Interactive authentication required.
suspending.sh[7585]: See system logs and 'systemctl status suspend.target' for details.
systemd[1]: suspend.service: Main process exited, code=exited, status=1/FAILURE
systemd[1]: suspend.service: Failed with result 'exit-code'.

这将回显Hello World,但不显示任何通知。。

怎么了?

您需要特殊访问权限才能发送通知:

suspending.sh[7585]: Failed to start suspend.target: Interactive authentication required.

懒惰的解决方案是用根替换<user>。安全选项是创建一个具有发送通知的正确权限的特定用户。

[Service]
Type=simple
ExecStart=/home/<user>/suspending.sh # Should not be in the home folder
User=<user>  # Should be a special user with permission

最新更新