Init.rb code stopping by itself + running headless

Hello,

After some trial and error I managed to find another way to run Sonic-Pi on startup that works pretty well. I realized that the shortcut Alt+R runs the currently loaded code in Sonic-Pi, all you need to do is to send the shortcut keystroke via command line after a delay on booth and the code will run.

I’m going to detail how I did it here in case someone finds that thread later on.

First step is to get Sonic Pi GUI to autostart following the instruction outline by Robin in his post

cd ~
mkdir -p .config/autostart
cp /usr/share/applications/sonic-pi.desktop   .config/autostart

This will get Sonic-Pi to boot automatically on start.

Next, install the keystroke tool xdotools (documentation here)

apt-get install xdotool

After, you will need to create a shell script file somewhere to write the xdotools commands to be executed on boot. I create mine on the desktop and called it autoplay.sh.

Navigate to desktop and create a new bash file:

sudo nano autoplay.sh

In the file enter these lines:

#!/bin/bash
export XAUTHORITY=/home/admin/.Xauthority; export DISPLAY=:0; xdotool key Return
sleep 60
xdotool key alt+r
  • line 1 = wizard magic line to make code work
  • line 2 = I’m not too sure why its needed but I had some errors trying to send keystroke and this fixed it. Found on this stackoverflow post. Also keep in mind that my user is called admin, if yours is the default Pi, change it in that line.
  • line 3 = Delay in seconds before the rest of the code executes, in this case 60 secs. This is so the RPI and Sonic-Pi have time to boot.
  • line 4 = The actual keystroke command that sends alt+r

Save/Exit nano

Now I don’t remember if I had to run the chmod command to make it executable but in any case here it is.

chmod +x autoplay.sh

You can follow more detailed instructions about shell script creation such as this one.

Next you will need to edit the rc.local file to add the script on startup. (more info here)

sudo nano /etc/rc.local

Add the following insctruction before the “exit 0” line

sh /home/admin/Desktop/autoplay.sh

Save/exit

Reboot your Pi and Sonic-Pi should run after 60 secs.

With this method you still need to manually copy the code and save. When Sonic-Pi boots, it loads the latest saved code. This doesn’t bother me since the idea is that once the project/code is done, I will copy one last time and save. After that I won’t need to change it and it will load and play automatically on boot.

Now there is still a weird bug where the BPM is slow a the start and slowly ramps up to the normal tempo, it’s annoying and hopefully there is a solution but if not I can work around it.

There you go, hope that helps someone!

Thanks