web-dev-qa-db-ja.com

再開時に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 bluetoothbluetooth.service/etc/systemd/system/bluetooth.serviceの別のコピーを作成したようですが、ファイルが保存されると(/lib/systemd/system/bluetooth.serviceとは)異なります。

bluetooth.serviceの2つの異なるバージョンがあることに気付いたので、少し混乱しました。

再開時にsystemdサービスを再起動する方法は?

3
SebMa
  • 新しい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
    
3
Gryu

/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

1
Gryu