# Exec Node, connection refused in service mode, ok when lnode red launched from terminal

**URL:** <https://discourse.nodered.org/t/exec-node-connection-refused-in-service-mode-ok-when-lnode-red-launched-from-terminal/76163>\
**Category:** General\
**Created:** [6 March 2023 07:05 UTC](https://discourse.nodered.org/t/exec-node-connection-refused-in-service-mode-ok-when-lnode-red-launched-from-terminal/76163 "2023-03-06T07:05:15Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![plouc](https://avatars.discourse-cdn.com/v4/letter/p/f19dbf/32.png) [@plouc](https://discourse.nodered.org/u/plouc)\
**Post date:** [6 March 2023 07:05 UTC](https://discourse.nodered.org/t/exec-node-connection-refused-in-service-mode-ok-when-lnode-red-launched-from-terminal/76163/1 "2023-03-06T07:05:15Z")

</div>

Hello,  
I have a flow executing commands to pulseaudio on a raspberry pi.  
The flow works great when i launch node-red from the terminal (trough ssh), but when node red run as a service it is doing this error:  
"  
Command failed: pactl list sink-inputs  
Connection failure: Connection refused  
pa\_context\_connect() failed: Connection refused  
"

I found this topic which i think is related:

> [@The exec node fails to execute a command that works in the terminal](https://discourse.nodered.org/t/the-exec-node-fails-to-execute-a-command-that-works-in-the-terminal/27897):
>
> Hello everyone and thank you for all your answers avaliable on this forum which allowed me to discover node-red and a little programming. I am a total newbie and my question may seem silly to you: What can prevent an exec node from executing a command that works in the terminal? Let me explain : I would like to "mute" and "unmute" the raspberry 3b sound output with an exec node (i use pulseaudio). I tried many command found on various sources but: no succes. For example, the following two co…

If i understood well this is "because pulseaudio requires an Xwindow environment to run"

So how should i change the nodered.service file to enable the Xwindow environnement in service mode?

Thanks a lot for your help, i'm pretty new to linux and any help will be much appreciated!

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [6 March 2023 13:52 UTC](https://discourse.nodered.org/t/exec-node-connection-refused-in-service-mode-ok-when-lnode-red-launched-from-terminal/76163/2 "2023-03-06T13:52:51Z")

</div>

Having done some research (ie google) it appears that it may be that if you want to access pulseaudio from node-red running as a system service then you will have to run pulseaudio as a system service too. Apparently it runs by default as a User service rather than a System service. I am working at the edge of my knowledge here though, so I might be wrong. As to how to run pulseaudio as a system service rather than a user service I don't know.

---

<div class="post-metadata">

**Author:** ![plouc](https://avatars.discourse-cdn.com/v4/letter/p/f19dbf/32.png) [@plouc](https://discourse.nodered.org/u/plouc)\
**Post date:** [6 March 2023 14:30 UTC](https://discourse.nodered.org/t/exec-node-connection-refused-in-service-mode-ok-when-lnode-red-launched-from-terminal/76163/3 "2023-03-06T14:30:00Z")

</div>

hi Colin and thanks for your answer.

Indeed, after questionning chatGPT for some time, it finally helped me to find a solution which is working!

If i understood well, it is indeed needed to start pulseaudio as a service.

So there is it's solution:

Modifying the file node-red.service with this content will make sure that pulseaudio runs as a service before starting node-red:

```auto
[Unit]
Description=Node-RED graphical event wiring tool
Wants=network.target
# here
After=pulseaudio.service
Documentation=http://nodered.org/docs/hardware/raspberrypi.html

[Service]
Type=simple
User=pi
Group=pi
WorkingDirectory=/home/pi

Environment="NODE_OPTIONS=--max_old_space_size=2048"
EnvironmentFile=-/home/pi/.node-red/EnvironmentFile

# and here
ExecStartPre=/usr/bin/pulseaudio --start
ExecStart=/usr/bin/env node-red-pi $NODE_OPTIONS $NODE_RED_OPTIONS
KillSignal=SIGINT
Restart=on-failure
RestartSec=20
SyslogIdentifier=Node-RED

[Install]
WantedBy=multi-user.target

```

at least it works for me!

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [20 March 2023 14:30 UTC](https://discourse.nodered.org/t/exec-node-connection-refused-in-service-mode-ok-when-lnode-red-launched-from-terminal/76163/4 "2023-03-20T14:30:15Z")

</div>

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