我认为我的 systemd 服务(在启动时关闭蓝牙)应该只在启动时运行?

我认为我的 systemd 服务(在启动时关闭蓝牙)应该只在启动时运行?

我编写了一个 systemd 服务来在启动时关闭蓝牙,但仍然希望能够在需要时通过蓝牙顶部面板小程序图标将其打开。

它在启动时很好地关闭了蓝牙,我想我已经测试过手动打开它是有效的。

最近我买了一些蓝牙音箱,我尝试连接它们。

但每次我打开蓝牙时,它都会再次关闭(大约十秒后),并且日志显示是我的服务在关闭。

(更让我困惑的是,我尝试连接的第一天,我设法连接到扬声器足够长的时间来播放几首交响乐 - 只是从第三天开始,蓝牙总是自动关闭。)

难道是我理解错了?我认为该服务只会在启动时运行一次。我还需要指定其他内容吗?

以下是该服务的最新版本。

如果我注释掉Type=oneshot,它的行为是一样的。我包含了 ,WantedBy=bluetooth.target因为它创建了一个从/etc/systemd/system/bluetooth.target.wants/turn-bluetooth-off.service到 的符号链接/etc/systemd/system/turn-bluetooth-off.service,如果符号链接丢失,该服务根本不会运行。

sudo rfkill unblock bluetooth打开蓝牙时并没有更好的效果。

[Unit]
Description=Service to always turn bluetooth off at system start time
After=bluetooth.service
After=NetworkManager.service
After=network-online.target

[Service]
Type=oneshot
ExecStart=/usr/local/bin/turn-bluetooth-off.sh

[Install]
WantedBy=multi-user.target
WantedBy=bluetooth.target

和文件/usr/local/bin/turn-bluetooth-off.sh

#!/bin/bash

date > /root/disk_space_report.txt
rfkill block bluetooth
du -sh /home/ >> /root/disk_space_report.txt

我跑

$ systemctl daemon-reload
$ systemctl enable turn-bluetooth-off.service
Created symlink from /etc/systemd/system/default.target.wants/turn-bluetooth-off.service to /etc/systemd/system/turn-bluetooth-off.service.

重新启动后journalctl --unit=bluetooth 显示(其中一些行为红色):

-- Logs begin at Mon 2020-06-08 17:18:28 CEST, end at Mon 2020-06-08 17:43:45 CEST. --
Jun 08 17:18:41 Otilia systemd[1]: Starting Bluetooth service...
Jun 08 17:18:41 Otilia bluetoothd[824]: Bluetooth daemon 5.37
Jun 08 17:18:41 Otilia systemd[1]: Started Bluetooth service.
Jun 08 17:18:41 Otilia bluetoothd[824]: Starting SDP server
Jun 08 17:18:42 Otilia bluetoothd[824]: Bluetooth management interface 1.13 initialized
Jun 08 17:18:44 Otilia bluetoothd[824]: Failed to obtain handles for "Service Changed" characteristic
Jun 08 17:18:44 Otilia bluetoothd[824]: Not enough free handles to register service
Jun 08 17:18:44 Otilia bluetoothd[824]: Error adding Link Loss service
Jun 08 17:18:44 Otilia bluetoothd[824]: Not enough free handles to register service
Jun 08 17:18:44 Otilia bluetoothd[824]: Not enough free handles to register service
Jun 08 17:18:44 Otilia bluetoothd[824]: Not enough free handles to register service
Jun 08 17:18:44 Otilia bluetoothd[824]: Current Time Service could not be registered
Jun 08 17:18:44 Otilia bluetoothd[824]: gatt-time-server: Input/output error (5)
Jun 08 17:18:44 Otilia bluetoothd[824]: Not enough free handles to register service
Jun 08 17:18:44 Otilia bluetoothd[824]: Not enough free handles to register service
Jun 08 17:18:44 Otilia bluetoothd[824]: Sap driver initialization failed.
Jun 08 17:18:44 Otilia bluetoothd[824]: sap-server: Operation not permitted (1)
Jun 08 17:19:38 Otilia bluetoothd[824]: Failed to obtain handles for "Service Changed" characteristic
Jun 08 17:19:38 Otilia bluetoothd[824]: Not enough free handles to register service
Jun 08 17:19:38 Otilia bluetoothd[824]: Error adding Link Loss service
Jun 08 17:19:38 Otilia bluetoothd[824]: Not enough free handles to register service
Jun 08 17:19:38 Otilia bluetoothd[824]: Not enough free handles to register service
Jun 08 17:19:38 Otilia bluetoothd[824]: Not enough free handles to register service
Jun 08 17:19:38 Otilia bluetoothd[824]: Current Time Service could not be registered
Jun 08 17:19:38 Otilia bluetoothd[824]: gatt-time-server: Input/output error (5)
Jun 08 17:19:38 Otilia bluetoothd[824]: Not enough free handles to register service
Jun 08 17:19:38 Otilia bluetoothd[824]: Not enough free handles to register service
Jun 08 17:19:38 Otilia bluetoothd[824]: Sap driver initialization failed.
Jun 08 17:19:38 Otilia bluetoothd[824]: sap-server: Operation not permitted (1)
Jun 08 17:19:39 Otilia bluetoothd[824]: Endpoint registered: sender=:1.48 path=/MediaEndpoint/A2DPSource
Jun 08 17:19:39 Otilia bluetoothd[824]: Endpoint registered: sender=:1.48 path=/MediaEndpoint/A2DPSink

journalctl --unit=turn-bluetooth-off显示:

-- Logs begin at Mon 2020-06-08 17:18:28 CEST, end at Mon 2020-06-08 17:43:45 CEST. --
Jun 08 17:18:51 Otilia systemd[1]: Starting Service to always turn bluetooth off at system start time...
Jun 08 17:21:21 Otilia systemd[1]: Started Service to always turn bluetooth off at system start time.

rfkill list all展示

0: Toshiba Bluetooth: Bluetooth
    Soft blocked: no
    Hard blocked: no
1: phy0: Wireless LAN
    Soft blocked: no
    Hard blocked: no
3: hci0: Bluetooth
    Soft blocked: yes
    Hard blocked: no

注意:有时hci0不会显示,软块会针对其他项目之一显示。

现在我尝试使用蓝牙顶部面板小程序打开蓝牙。小程序显示 ON,然后大约十秒后再次显示 OFF。

新输出来自journalctl --unit=bluetooth

Jun 08 20:43:29 Otilia bluetoothd[824]: Failed to obtain handles for "Service Changed" characteristic
Jun 08 20:43:29 Otilia bluetoothd[824]: Not enough free handles to register service
Jun 08 20:43:29 Otilia bluetoothd[824]: Error adding Link Loss service
Jun 08 20:43:29 Otilia bluetoothd[824]: Not enough free handles to register service
Jun 08 20:43:29 Otilia bluetoothd[824]: Not enough free handles to register service
Jun 08 20:43:29 Otilia bluetoothd[824]: Not enough free handles to register service
Jun 08 20:43:29 Otilia bluetoothd[824]: Current Time Service could not be registered
Jun 08 20:43:29 Otilia bluetoothd[824]: gatt-time-server: Input/output error (5)
Jun 08 20:43:29 Otilia bluetoothd[824]: Not enough free handles to register service
Jun 08 20:43:29 Otilia bluetoothd[824]: Not enough free handles to register service
Jun 08 20:43:29 Otilia bluetoothd[824]: Sap driver initialization failed.
Jun 08 20:43:29 Otilia bluetoothd[824]: sap-server: Operation not permitted (1)
Jun 08 20:43:29 Otilia bluetoothd[824]: Endpoint registered: sender=:1.48 path=/MediaEndpoint/A2DPSource
Jun 08 20:43:29 Otilia bluetoothd[824]: Endpoint registered: sender=:1.48 path=/MediaEndpoint/A2DPSink
Jun 08 20:43:31 Otilia bluetoothd[824]: Endpoint unregistered: sender=:1.48 path=/MediaEndpoint/A2DPSource
Jun 08 20:43:31 Otilia bluetoothd[824]: Endpoint unregistered: sender=:1.48 path=/MediaEndpoint/A2DPSink

新输出来自journalctl --unit=turn-bluetooth-off

Jun 08 20:43:30 Otilia systemd[1]: Starting Service to always turn bluetooth off at system start time...
Jun 08 20:43:33 Otilia systemd[1]: Started Service to always turn bluetooth off at system start time.

rfkill list all展示

0: Toshiba Bluetooth: Bluetooth
    Soft blocked: yes
    Hard blocked: no
1: phy0: Wireless LAN
    Soft blocked: no
    Hard blocked: no

我看不出是什么导致我的服务再次运行。帮助!

答案1

好的,我找到了解决方法。我刚刚意识到关机时的蓝牙设置(打开或关闭)会在下次启动时保留。所以我根本不需要我的服务。笑脸。

普通蓝牙功能不太好,但我可以忍受。

相关内容