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

如何在恢复时重新启动 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

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.

EDIT2:看来该命令sudo systemctl edit --full bluetooth创建了 的另一个副本,一旦文件被保存,bluetooth.service它就/etc/systemd/system/bluetooth.service变得与 不同了。/lib/systemd/system/bluetooth.service

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

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

答案1

  • 创建新的 systemd 服务:

    sudo vim.tiny /lib/systemd/system/blrestart.service
    sudo ln -s /lib/systemd/system/blrestart.service /etc/systemd/system/
    

    粘贴其旁边的内容:

    [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
    
  • 启用并启动新创建的服务:

    sudo systemctl enable blrestart && sudo systemctl start blrestart
    

答案2

尝试使用/usr/lib/pm-utils/sleep.d 在那里创建脚本

# 70yourservice
case $1 in
    hibernate)
        echo "Hey guy, we are going to suspend to disk!"
        ;;
    suspend)
        echo "Oh, this time we are doing a suspend to RAM. Cool!"
        ;;
    thaw)
        echo "Oh, suspend to disk is over, we are resuming..."
        ;;
    resume)
        systemctl restart yourservice.service
        ;;
    *)  echo "Somebody is calling me totally wrong."
        ;;
esac

别忘了sudo chmod +x /usr/lib/pm-utils/sleep.d/70yourservice

相关内容