Service called from a script

Hi,

This is my very first script and I need help.

I want to run it on startup. It works…

Problem is that it calls the bluetooth service which triggers a window for the password.

The idea is to automate a connection to my keyboard which is on bluetooth, which makes it difficult to enter a password or to do anything on startup.

blueberry #the only visual tool that works for me, useful for connecting to my headset eventually later
sleep 5
cpt=0
while ((cpt<2)) #restart the bluetooth service
do
systemctl restart bluetooth #asks for password!
sleep 1
bluetoothctl connect 2B:24:13:DB:7C:99 #my keyboard
sleep 1
((cpt+=1))
done
systemctl discoverable-timeout 3 #let’s try that also if connection has not been made yet
exit 0

Before writing this script, I tested my bluetooth from the console many times and I could figure out that restarting the service could help.

From the archived forum

1 Like

Thank you!

So after my first bash, you invite me to writing my first service :relaxed:
It took me 2 hours to grasp the concept, but why not with some more hard work.

EDIT: I installed Manjaro a month ago. Had never heard of Arch, pacman, etc. Wish me good luck :stuck_out_tongue_winking_eye: I love Manjaro.

1 Like

Actually it’s not difficult to understand and the links provided make it fun, as you learn from ‘live’ stories.

The first test I made out of it is modifying the bluetooth service file: I removed the # sign before Restart=on-failure. (anyway systemctl status bluetooth indicates it runs on startup, it has never failed).

I still need to restart the service manually. Even my attempts to restart from my script generally fail to allow a successful bluetoothctl connect 2B:24:13:DB:7C:99

I need to elaborate a sequence that works.

For the time being, I always need my other keyboard not far away…

EDIT: I’ve finetuned the script but the resuslts are still very erratic. Bluetooth is a mess and I am not sure writing a systemd service will help in this case. Thanks anyway.

I gave it a first try with

#/etc/systemd/system/bt-restart.service

[Unit]
Description=restart bt and connect keypad

[Service]
Type=oneshot
User=root

RemainAfterExit=yes

ExecStart=/bin/systemctl restart bluetooth
ExecStart=/usr/bin/sleep 3
ExecStart=/home/jcw/bin/enable-bt.sh

[Install]
WantedBy=multi-user.target

and

#!/bin/bash

#/home/jcw/bin
#appelé par bt-restart.service

bluetoothctl connect 2B:24:13:DB:7C:99 #le clavier
sleep 10
blueberry #lance le GUI
exit 0

My final step

#/etc/systemd/system/bt-restart.service

[Unit]
Description=restart bt and discover keypad
After=network-online.target
Wants=bluetooth.target
StopWhenUnneeded=yes

[Service]
Type=oneshot
User=root

RemainAfterExit=yes

ExecStart=/bin/modprobe btusb
ExecStart=/usr/bin/sleep 80
ExecStart=/home/jcw/bin/enable-bt.sh
ExecStart=/bin/systemctl restart bluetooth
ExecStart=/bin/bluetoothctl discoverable-timeout 3

[Install]
WantedBy=multi-user.target

In enable-bt.sh I don’t attempt to connect to my keyboard any more. Instead I test the connection to my headset, so that I can check bluetooth. It works fine for my headset and I did not test for anything else, but for some reason I need to restart bluetooth for my keyboard.

If you have comments on my systemd script, bear in mind that I am still learning.

NB: now I am still looking for a graphic tool similar to blueberry or blueman, the one provided by Manjaro does not display anything!

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.