1

I am following this tutorial on using a mobile phone's accelerometer.

In order for it to work properly you have to run three commands on startup every time...

rfkill unblock bluetooth
killall bluetoothd
hciconfig hci0 up

Is there a way to do this with a script on startup instead of manually doing it every time?

2 Answers2

1

Most systems will automatically load /etc/rc.local on startup when present, so you just have to put your commands in there and that should do the trick.

You should make sure it is executable by root, but it probably already is.

  • I am really keen to try this out, I am currently putting all my system startup commands in my ~/.zshrc file. as all the other places which are often recommended don't always work. do you know if /etc/rc.local is run late in boot, i.e. after all the graphical system is ready? for example, could I put xset commands targeting X server in there? – the_velour_fog Aug 26 '16 at 01:30
  • @the_velour_fog: The .zshrc file will run once per session, not per boot. This is not suitable for every command you need to run on startup. I honestly don't know for your question on xset though, but it should take you 5-10 minutes to find out. – Julie Pelletier Aug 26 '16 at 01:33
  • thanks, yes running the system startup commands every time I launch a new shell is overkill, but until now no other solution has worked. The other problem, is the system startup commands don't get run at all until I have launched a shell. Unfortunately I can't simply reboot to test, I've got too many daemons that won't restart, text editor sessions that might get lost and USB device plugged in which will lose state if I reboot – the_velour_fog Aug 26 '16 at 01:47
  • @the_velour_fog: In my experience, when you're in such a situation is when you should put in efforts to properly automate your startup or otherwise it can quickly become a nightmare. – Julie Pelletier Aug 26 '16 at 01:49
  • yes, well I will try your /etc/rc.local solution on next reboot, and hopefully that will get me one step closer to having a sane system! – the_velour_fog Aug 26 '16 at 01:56
  • @the_velour_fog /etc/rc.local is run as one of the last things, but still before the X server. If you want to run things when the X server starts, you have to modify a file that depends on how X starts — which display manager loads it. – Gilles 'SO- stop being evil' Aug 26 '16 at 22:01
  • @Gilles thanks. Also Im guessing that its not enough to setup the custom startup commands so that - chronologically - they run after the xserver. but that the process that fork/execs the shell that runs those commands has descended from processes that initialised environment variables those custom startup commands would need? – the_velour_fog Aug 26 '16 at 22:20
  • @the_velour_fog Yes, if something requires X you need to run it with the right DISPLAY environment variable and possibly XAUTHORITY. http://unix.stackexchange.com/questions/10121/open-a-window-on-a-remote-x-display-why-cannot-open-display/10126#10126 – Gilles 'SO- stop being evil' Aug 26 '16 at 22:26
  • I cant seem to find rc.local – Matthew Jackaon Sep 02 '16 at 00:11
0

If your system uses systemd, you could write a oneshot service that executes the command once, at system boot.

First, create a file /opt/scripts/configure-bluetooth.sh and put your commands in:

#!/bin/bash
rfkill unblock bluetooth
killall bluetoothd
hciconfig hci0 up

And make the file executable: chmod +x /opt/scripts/configure-bluetooth.sh

The create a new service unit: /etc/systemd/system/configure-bluetooth.service That contains:

[Unit]
Description=Configure bluetooth

[Service]
Type=oneshot
ExecStart=/opt/scripts/configure-bluetooth.sh

[Install]
WantedBy=multi-user.target

You should now run systemctl daemon-reload so that systemd detects the new service. To test it, run systemctl start configure-bluetooth.service.

Once you are sure that it works, you can enable it on boot with: systemctl enable configure-bluetooth.service

Bram
  • 21
  • 3
  • It works but every time I reboot the Edison I have to run the start command. I have tried multiple times to set this up but seems as though I doesn't automatically start. – Matthew Jackaon Aug 31 '16 at 14:55
  • I was probably wrong on the WantedBy target, see this link. Could you try to change WantedBy=multi-user.target to WantedBy=default.target? You might also need to use systemctl --user enable configure.bluetooth as described there. – Bram Sep 01 '16 at 17:18
  • I tried and after systemctl --user... I got "Failed to get D-Bus connection: Connection refused" – Matthew Jackaon Sep 02 '16 at 00:10