You are not logged in.

#1 2023-11-10 22:27:37

860lacov
Member
Registered: 2020-05-02
Posts: 497

Systemd timer and service to automatically stop bluetooth

I wanted to create a service that will turn off Bluetooth in my laptop if no device is connected for 10 minutes

cat /etc/systemd/system/disable-bluetooth.service 
[Unit]
Description=Disable Bluetooth if no devices are connected and running on battery

[Service]
Type=oneshot
ExecStart=/usr/bin/bash -c 'devices_connected=$(bluetoothctl devices Connected | wc -l); power_status=$(cat /sys/class/power_supply/AC/online); if [ "$devices_connected" -eq 0 ] && [ "$power_status" -eq 0 ]; then systemctl stop bluetooth; fi'

Tried with

ExecStart=/usr/bin/bash -c 'bluetooth_status=$(bluetoothctl info | grep "Connected: yes"); power_status=$(cat /sys/class/power_supply/AC/online); if [ -z "$bluetooth_status" ] && [ "$power_status" -eq 0 ]; then systemctl stop bluetooth; fi'

Just to be sure

cat /etc/systemd/system/disable-bluetooth.timer 
[Unit]
Description=Run Disable Bluetooth service every 10 minutes

[Timer]
OnUnitInactiveSec=10m
Unit=disable-bluetooth.service

[Install]
WantedBy=timers.target

It doesn't work.
I use KDE Plasma.
When I do the test with
systemctl start disable-bluetooth.service
I can see that Bluetooth icon on the taskbar disappear for a fraction of a second and it shows up immediately

Last edited by 860lacov (2023-11-10 22:49:39)

Offline

#2 2023-11-11 04:48:44

c00ter
Member
From: Alaskan in Washington State
Registered: 2014-08-28
Posts: 421

Re: Systemd timer and service to automatically stop bluetooth

Why not a timer linked to something incorporating # systemctl stop && disable bluetooth instead of "stop"? Same with "enable && start" instead of "start".

Last edited by c00ter (2023-11-11 04:54:07)


UNIX was not designed to stop you from doing stupid things, because that would also stop you from doing clever things. -- Doug Gwyn

Offline

#3 2023-11-11 08:27:18

860lacov
Member
Registered: 2020-05-02
Posts: 497

Re: Systemd timer and service to automatically stop bluetooth

c00ter wrote:

Why not a timer linked to something incorporating # systemctl stop && disable bluetooth instead of "stop"? Same with "enable && start" instead of "start".

To be honest I don't understand what's on your mind

Offline

Board footer

Powered by FluxBB