8

I have the following service configuration :

$ 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

I added the suspend.target in the WantedBy= parameter of the [Install] section (man systemd.unit) for the service to be restarted upon resume but this does not work.

EDIT0 : The service is not stopped at suspend but it needs to be restarted after resume because this service does not support suspend/resume operation on my configuration.

EDIT1 : The suspend.target should also work because according to man systemd.special :

   suspend.target
       A special target unit for suspending the system. This pulls in sleep.target.

EDIT2 : It seems that the command sudo systemctl edit --full bluetooth created another copy of the bluetooth.service in /etc/systemd/system/bluetooth.service which became different (from /lib/systemd/system/bluetooth.service) once the file was saved.

I've just noticed I had two different versions of bluetooth.service so I was a little confused.

How to restart a systemd service upon resume ?

SebMa
  • 2,035

2 Answers2

8
  • Create the new systemd service:

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

    Paste the next to it:

    [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
    
  • Enable and start the newly created service:

    sudo systemctl enable blrestart && sudo systemctl start blrestart
    
Gryu
  • 280
1

Try using /usr/lib/pm-utils/sleep.d creating your scripts there:

# 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

Don't forget sudo chmod +x /usr/lib/pm-utils/sleep.d/70yourservice

Gryu
  • 280