# How to spawn C code using exec

**URL:** https://discourse.nodered.org/t/how-to-spawn-c-code-using-exec/99273
**Category:** General
**Created:** [5 October 2025 16:11 UTC](https://discourse.nodered.org/t/how-to-spawn-c-code-using-exec/99273 "2025-10-05T16:11:20Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![pedro\_uno](https://avatars.discourse-cdn.com/v4/letter/p/bcef8e/32.png) [@pedro\_uno](https://discourse.nodered.org/u/pedro_uno)
#### Post date: [5 October 2025 16:11 UTC](https://discourse.nodered.org/t/how-to-spawn-c-code-using-exec/99273/1 "2025-10-05T16:11:20Z")

</div>

Hello,  
I am having trouble launching a C program in spawn mode using an exec node.

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/e/7/e752fbb7998651889d89a7bf0d97bd5a02718363.png)

I created a simplified example where a button starts the exec node. Unfortunately when I run it, I always get an error with return code of -2.

I have this little example committed here: [GitHub - hdlguy/node-red\_experiments](https://github.com/hdlguy/node-red_experiments.git)

The C code is just a loop that generates some text output and then returns 0.

```auto
#include <stdio.h>
#include <unistd.h>
int main()
{
    for(int i=0; i<=100; i+=20) {
        fprintf(stdout, "%d\n", i);
        fflush(stdout);
        usleep(500000);
    }    
    return(0);
}

```

---

<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: [5 October 2025 16:34 UTC](https://discourse.nodered.org/t/how-to-spawn-c-code-using-exec/99273/2 "2025-10-05T16:34:35Z")

</div>

Add debug nodes to all outputs of the exec node, set them to Output Complete Message and check all properties of the outputs. There will probably be a helpful error message there.

Have you specified the full path to the executable file?

I don't see the flow in your repository.

---

<div class="post-metadata">

### Author: ![hardillb](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hardillb/32/12373_2.png) [@hardillb](https://discourse.nodered.org/u/hardillb)
#### Post date: [5 October 2025 16:35 UTC](https://discourse.nodered.org/t/how-to-spawn-c-code-using-exec/99273/3 "2025-10-05T16:35:18Z")

</div>

You've not checked in the flow file so we have no idea how you've configured the exec node.

Also you probably want to swap the dashboard start button for a inject node to get started and set the debug node to whole message not just payload to see more.

You should also check the Node-RED logs for more error messages.

As it is there isn't really anything we can help with so far.

And I assume you have tested running the compiles c program from the command line first?

The code when complied and run with a full path works just fine on my Ubuntu box with NR 4.1.0

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/8/383b8d2c093d624d6d6743f01e6f562db201492f.png)

---

<div class="post-metadata">

### Author: ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)
#### Post date: [5 October 2025 17:09 UTC](https://discourse.nodered.org/t/how-to-spawn-c-code-using-exec/99273/4 "2025-10-05T17:09:05Z")

</div>

~~Please don't be offended by the question, but your exec node is trying to run the compiled executable, not the C source code?~~ Edit: I think that would give rc -13

Does it make a difference if you specify the full pathname of the executable eg `/home/pedro/.node-red/bin/gen_percent` (or wherever the file is)?  
One cause of rc -2 is that the OS cannot find the file to execute it.

---

<div class="post-metadata">

### Author: ![pedro\_uno](https://avatars.discourse-cdn.com/v4/letter/p/bcef8e/32.png) [@pedro\_uno](https://discourse.nodered.org/u/pedro_uno)
#### Post date: [6 October 2025 12:58 UTC](https://discourse.nodered.org/t/how-to-spawn-c-code-using-exec/99273/5 "2025-10-06T12:58:57Z")

</div>

The flow is checked in here, [node-red\_experiments/c2progress/.node-red at main · hdlguy/node-red\_experiments · GitHub](https://github.com/hdlguy/node-red_experiments/tree/main/c2progress/.node-red), but it is a hidden folder so sometimes I also miss seeing it.

I will try the an inject node but I actually want have the code started by a user hitting a button. I think I have tried several forms of the path. I will try to hard code the full path again. That should be ok since the application is an embedded system. I don't really need relative path names.

I will also change the debug node to whole messages.

I was about to give up on this but since you guys are so helpful, I will keep trying.

---

<div class="post-metadata">

### Author: ![hardillb](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hardillb/32/12373_2.png) [@hardillb](https://discourse.nodered.org/u/hardillb)
#### Post date: [6 October 2025 13:34 UTC](https://discourse.nodered.org/t/how-to-spawn-c-code-using-exec/99273/6 "2025-10-06T13:34:31Z")

</div>

The flow is not in that directory

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/f/c/fc0decc58cd1bf02ac3b523a7c58de671ef73b47.png)

---

<div class="post-metadata">

### Author: ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)
#### Post date: [6 October 2025 14:00 UTC](https://discourse.nodered.org/t/how-to-spawn-c-code-using-exec/99273/7 "2025-10-06T14:00:27Z")

</div>

> [@hardillb](#):
>
> The flow is not in that directory

The code which @pedro_uno included in his original post is at [https://github.com/hdlguy/node-red\_experiments/blob/main/c2progress/c\_code/gen\_percent.c](https://github.com/hdlguy/node-red_experiments/blob/main/c2progress/c_code/gen_percent.c)

---

<div class="post-metadata">

### Author: ![hardillb](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hardillb/32/12373_2.png) [@hardillb](https://discourse.nodered.org/u/hardillb)
#### Post date: [6 October 2025 14:12 UTC](https://discourse.nodered.org/t/how-to-spawn-c-code-using-exec/99273/8 "2025-10-06T14:12:14Z")

</div>

@jbudd I'm not interested in the C code, I have that, I want to see the Node-RED flow so I can actually see how the exec node is configured (e.g. is the full path to the executable provided)

---

<div class="post-metadata">

### Author: ![pedro\_uno](https://avatars.discourse-cdn.com/v4/letter/p/bcef8e/32.png) [@pedro\_uno](https://discourse.nodered.org/u/pedro_uno)
#### Post date: [6 October 2025 14:35 UTC](https://discourse.nodered.org/t/how-to-spawn-c-code-using-exec/99273/9 "2025-10-06T14:35:25Z")

</div>

OK, thanks again for the attention to my problem. I have now committed flows.json to the repo. I also got the got the flow to run with the exec node in spawn mode. The trick was to use an absolute path to the compiled C command.

My purpose was to use the exec code in spawn mode so that it could drive a progress bar during flash memory programming stages. In addition to the absolute path, I needed to put a fflush(stdout) command after each printf of the C code. I also needed to use a change block to convert the string to a number. All these changes were the result of suggestions on this forum.

Thanks for all the support and thanks for node-red itself. I do embedded design involving FPGAs and I need a way to create web UIs without being a web developer. Node-Red is really working out for me.

---

<div class="post-metadata">

### Author: ![pedro\_uno](https://avatars.discourse-cdn.com/v4/letter/p/bcef8e/32.png) [@pedro\_uno](https://discourse.nodered.org/u/pedro_uno)
#### Post date: [9 October 2025 14:01 UTC](https://discourse.nodered.org/t/how-to-spawn-c-code-using-exec/99273/10 "2025-10-09T14:01:17Z")

</div>

I uploaded a video of the DB1 chart node displaying a rapidly updating waveform. It is 600KB but it shows what is possible.

```auto
https://github.com/hdlguy/node-red_experiments/blob/main/db2_chart/db1_waveform_performance.webm

```
