Skip to content

child_process.fork and child_process.exec do not work together. #10412

@JMishou

Description

@JMishou
var child_process = require('child_process');

var failed_wifi_reconnect_attempts = 0;

var checkWiFi = setInterval(WiFiCheck, 10000);

function WiFiCheck(){
    child = child_process.exec('ping -c 1 192.168.0.1', function(error, stdout, stderr){
        if(error) {
            console.log("Offline - Attempting to bring up wlan0")
            child_process.exec('ifup --force wlan0',function(error, stdout, stderr){
                console.log("error: " + error);
                console.log("stdout: " + stdout);
                console.log("stderr: " + stderr);
                if(error || stderr !== ""){
                    failed_wifi_reconnect_attempts++;
                    console.log("Failed to bring up wlan0\n" + stderr);
                    if (failed_wifi_reconnect_attempts == 5){
                        child_process.exec('shutdown -r now',function(error, stdout, stderr){
                            console.log(stdout);                        
                        });
                    }
                }
                else{
                    console.log("wlan0 back online");
                }
            });
        }
        else {
            console.log("Online")
        }
    });
}

Version: 4.5.0
Platform: Raspberry Pi Zero Linux 4.4.21+ #911 armv6l GNU/Linux
Subsystem: Raspian Jesse Lite

I have created a test script to check and restore a wifi connection on the raspberry pi. It works fine when tested by itself. However when I add it to the application that I want it to be integrated with it fails to execute. The issue seems to be that I have a forked process also running from the child_process library. If I remove the fork from the main app and leave the exec code it again works as it did in the test script.

Metadata

Metadata

Assignees

No one assigned

    Labels

    child_processIssues and PRs related to the child_process subsystem.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions