# Init.rb code stopping by itself + running headless

**URL:** <https://in-thread.sonic-pi.net/t/init-rb-code-stopping-by-itself-running-headless/8047>\
**Category:** Support, Help & Resources\
**Created:** [August 6, 2023, 4:24pm UTC](https://in-thread.sonic-pi.net/t/init-rb-code-stopping-by-itself-running-headless/8047 "2023-08-06T16:24:54Z")\
**Posts on this page:** 1\
**Showing post:** 7

<div class="post-metadata">

**Author:** ![mathieud](https://avatars.discourse-cdn.com/v4/letter/m/91b2a8/32.png) [@mathieud](https://in-thread.sonic-pi.net/u/mathieud)\
**Post date:** [August 28, 2023, 1:06am UTC](https://in-thread.sonic-pi.net/t/init-rb-code-stopping-by-itself-running-headless/8047/7 "2023-08-28T01:06:35Z")

</div>

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](https://in-thread.sonic-pi.net/t/sonic-pi-for-standalone-installations/225/4)

```ruby
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](https://github.com/jordansissel/xdotool) ([documentation here](https://manpages.ubuntu.com/manpages/trusty/man1/xdotool.1.html))

```ruby
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:

```ruby
sudo nano autoplay.sh

```

In the file enter these lines:

```ruby
#!/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](https://stackoverflow.com/questions/38032012/xdotool-fails-on-raspberry-pi) 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.

```ruby
chmod +x autoplay.sh

```

You can follow more detailed instructions about shell script creation such as [this one](https://www.geeksforgeeks.org/how-to-create-a-shell-script-in-linux/#).

Next you will need to edit the rc.local file to add the script on startup. (more info [here](https://raspberrypi-guide.github.io/programming/run-script-on-boot))

```ruby
sudo nano /etc/rc.local

```

Add the following insctruction before the “exit 0” line

```ruby
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

---

_[View the full topic](https://in-thread.sonic-pi.net/t/init-rb-code-stopping-by-itself-running-headless/8047)._
