Curl via runShellCommandAsynchronously -> Zombie?

Hi,

I am using a Yun to run a watering pump depending on the soil humidity in my greenhouse. Whilst this is working fine, I would like to be able to check the values when not being at home. So I am sending data to thingspeak. It's crucial that the sketch runs regardless of potential network dropouts, so I thought it would be a good idea to upload data on the Linux side (via curl) in a non-blocking way. I am new to Arduino but I guess that runShellCommandAsynchronously() should do the job.

Here is a short example

#include <Process.h>

Process p;
String command_string = "";
long THINGSPEAK_INTERVAL = 60000;
long lastThingspeakUpdate = 0;

void setup()
{
//...
}


void loop()
{
  // control the watering
    
  // Send data to ThingSpeak
  if((millis() - lastThingspeakUpdate) >= THINGSPEAK_INTERVAL){
    command_string = "curl --data \"api_key=";
    command_string += String(myWriteAPIKey);
    command_string += "&field1=";
    //assembly of curl command
    command_string+= "\" https://api.thingspeak.com/update";
    p.runShellCommandAsynchronously(command_string);
    //delay(10000);
    //p.close();
    lastThingspeakUpdate = millis();
  }
}

This works fine but after each run it leaves curl as a zombie process on the Linux side. That zombie gets killed when a new data upload is due but it feels as if I am doing something wrong. Do I need to close the process? I thought about a p.close() after a delay but on the other hand this seems to contradict the idea of a process running in the background. When I run curl from the prompt it runs and finishes within a second.

Should I worry about the zombie?

Yes and no. A zombie is a process that finished but it's result code wasn't read by it's controlling process.
A zombie process still uses some resources (although a lot less than while running) so you should avoid producing it.

You should introduce a boolean variable that represents if the process is still active. Set it to true when you start the command. Check p.running() in every loop() run while the process is active and once p.running() gets false call p.exitValue() to get the response code. That way no zombies should be created anymore.

1 Like

Excellent - that worked! Thanks for explaining it so well.

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