# Run-code not working in headless mode

**URL:** <https://in-thread.sonic-pi.net/t/run-code-not-working-in-headless-mode/7635>\
**Category:** Support, Help & Resources\
**Created:** [March 10, 2023, 8:09am UTC](https://in-thread.sonic-pi.net/t/run-code-not-working-in-headless-mode/7635 "2023-03-10T08:09:20Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![dwjbosman1](https://avatars.discourse-cdn.com/v4/letter/d/bc8723/32.png) [@dwjbosman1](https://in-thread.sonic-pi.net/u/dwjbosman1)\
**Post date:** [March 10, 2023, 8:09am UTC](https://in-thread.sonic-pi.net/t/run-code-not-working-in-headless-mode/7635/1 "2023-03-10T08:09:20Z")

</div>

I would like to use Sonic Pi as a backend/synth engine for my own GUI. I have been looking into using the OSC run-code message.

I first try was to use SonicPipe. I first started Sonic-Pi (non-headless) and looked up the ports and security token in spider.log. Fixing some bugs in SonicPipe I was able to execute code via Sonic Pi, so I am hearing the sounds when running the following Python script:

from sonic\_pipe import SonicPipe  
import time  
sonic\_path = “/opt/sonic-pi/app/server/ruby/bin/daemon.rb”

pi = SonicPipe(use\_daemon=False,daemon\_rb\_location=sonic\_path)  
pi.pipe(“play :c, amp: 1”)  
time.sleep(0.5)  
pi.pipe(“play :c, amp: 1”)

This means that run-command is correctly received by Sonic Pi.

Next step was to run the same code but now in headless mode (use\_daemon=True) which uses the daemon.rb script:  
pi = SonicPipe(use\_daemon=Truedaemon\_rb\_location=sonic\_path)  
…  
I see that Sonic Pi is started, and spider.log shows which ports to use. Adding some debugging info to SonicPipe also shows that it is sending the run-command OSC message to the correct port and with the correct token. However no sound can be heard.

Any idea what could be the problem?

Is there maybe a log file which shouws which OSC messages were received?

---

<div class="post-metadata">

**Author:** ![samaaron](https://dub1.discourse-cdn.com/flex017/user_avatar/in-thread.sonic-pi.net/samaaron/32/7_2.png) [@samaaron](https://in-thread.sonic-pi.net/u/samaaron)\
**Post date:** [March 10, 2023, 2:00pm UTC](https://in-thread.sonic-pi.net/t/run-code-not-working-in-headless-mode/7635/2 "2023-03-10T14:00:42Z")

</div>

Apologies, I have no direct experience of using Python to drive Sonic Pi. However, I can say that using `daemon.rb` is the best way of working towards using Sonic Pi headless and that if you don’t use Sonic Pi’s language for timing, you are missing out on pretty much all of the hard work I’ve put into the timing system. The timing will drift and not be accurate, work poorly across multiple threads (not be in sync) and not benefit from the Ableton Link metronome.

Python’s `time.sleep(0.5)` is _not at all_ the same as Sonic Pi’s `sleep 0.5` even though they look very similar.

---

<div class="post-metadata">

**Author:** ![dwjbosman1](https://avatars.discourse-cdn.com/v4/letter/d/bc8723/32.png) [@dwjbosman1](https://in-thread.sonic-pi.net/u/dwjbosman1)\
**Post date:** [March 10, 2023, 8:12pm UTC](https://in-thread.sonic-pi.net/t/run-code-not-working-in-headless-mode/7635/3 "2023-03-10T20:12:27Z")

</div>

Thanks for your answer. In this case I am using python purely to send run-code commands. Especially because as you mention, I want to make use of Sonic Pi’s timing capabilities. Using sleep was just to test…

The current problem I am having is:

The run-command is correctly processed by sonic pi if I send it to a normally started running sonic pi instance which includes a gui.

The run-command is not processed somehow if I send it to a sonic pi which was started headless using daemon.rb. But maybe something else is wrong. Is there a debug flag or something with which I could see if osc messages are received and maybe I can see if for example the token is incorrect?

---

<div class="post-metadata">

**Author:** ![samaaron](https://dub1.discourse-cdn.com/flex017/user_avatar/in-thread.sonic-pi.net/samaaron/32/7_2.png) [@samaaron](https://in-thread.sonic-pi.net/u/samaaron)\
**Post date:** [March 10, 2023, 9:09pm UTC](https://in-thread.sonic-pi.net/t/run-code-not-working-in-headless-mode/7635/4 "2023-03-10T21:09:25Z")

</div>

There’s no debug flag as such but if you modify the following functions to return true, you’ll see a lot more debug messages in the logs - including incoming OSC messages 🙂

> <https://github.com/sonic-pi-net/sonic-pi/blob/dev/app/server/ruby/lib/sonicpi/util.rb#L335>

---

<div class="post-metadata">

**Author:** ![dwjbosman1](https://avatars.discourse-cdn.com/v4/letter/d/bc8723/32.png) [@dwjbosman1](https://in-thread.sonic-pi.net/u/dwjbosman1)\
**Post date:** [March 11, 2023, 1:51pm UTC](https://in-thread.sonic-pi.net/t/run-code-not-working-in-headless-mode/7635/5 "2023-03-11T13:51:17Z")

</div>

In the end I needed to wait longer (about 6 seconds on an RPI4) before Sonic Pi is ready to process the run-command OSC mesaages.
