如何在恢复时重新启动 systemd 服务

Seb*_*bMa 3 linux suspend systemd

我有以下服务配置:

$ systemctl cat bluetooth
# /lib/systemd/system/bluetooth.service
[Unit]
Description=Bluetooth service
Documentation=man:bluetoothd(8)
ConditionPathIsDirectory=/sys/class/bluetooth

[Service]
Type=dbus
BusName=org.bluez
ExecStart=/usr/lib/bluetooth/bluetoothd
NotifyAccess=main
#WatchdogSec=10
#Restart=on-failure
CapabilityBoundingSet=CAP_NET_ADMIN CAP_NET_BIND_SERVICE
LimitNPROC=1
#RestartSec=5

[Install]
WantedBy=bluetooth.target suspend.target
Alias=dbus-org.bluez.service
Run Code Online (Sandbox Code Playgroud)

suspend.target在( )部分的WantedBy=参数中添加了要在恢复时重新启动的服务,但这不起作用。[Install]man systemd.unit

EDIT0 :该服务在挂起时不会停止,但需要在恢复后重新启动,因为该服务不支持对我的配置进行挂起/恢复操作。

EDIT1:suspend.target也应该工作,因为根据man systemd.special

   suspend.target
       A special target unit for suspending the system. This pulls in sleep.target.
Run Code Online (Sandbox Code Playgroud)

EDIT2 :似乎该命令sudo systemctl edit --full bluetooth创建了另一个副本,一旦文件被保存bluetooth.service/etc/systemd/system/bluetooth.service其中就变得不同(从/lib/systemd/system/bluetooth.service)。

我刚刚注意到我有两个不同的版本,bluetooth.service所以我有点困惑。

如何在恢复时重新启动 systemd 服务?

Gry*_*ryu 5

  • 创建新的 systemd 服务:

    sudo vim.tiny /lib/systemd/system/blrestart.service
    sudo ln -s /lib/systemd/system/blrestart.service /etc/systemd/system/
    
    Run Code Online (Sandbox Code Playgroud)

    粘贴旁边的:

    [Unit]
    Description=Restart Bluetooth after resume
    After=suspend.target
    
    [Service]
    Type=simple
    ExecStart=/bin/systemctl --no-block restart bluetooth.service
    # ExecStart=/usr/bin/systemctl --no-block restart bluetooth.service
    
    [Install]
    WantedBy=suspend.target
    
    Run Code Online (Sandbox Code Playgroud)
  • 启用并启动新创建的服务:

    sudo systemctl enable blrestart && sudo systemctl start blrestart
    
    Run Code Online (Sandbox Code Playgroud)