Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6776611
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T16:02:59+00:00 2026-05-26T16:02:59+00:00

I’m quite new to node and am trying to create something that gets some

  • 0

I’m quite new to node and am trying to create something that gets some server info. But here’s my problem. I setup a config object (this will, in time, become updated dynamically by events that occur) and then later in, in a function, I try and access a value in this object. (See code below)

So First, I setup my vars:

var util            = require('util'),
    child           = require('child_process'),
    config          = {};

which works okay. Then I load my config:

function loadConfig( )
{
    // Add some code for auto-loading of args
    config = {
        "daemons": [
            ["Apache", "apache2"],
            ["MySQL",  "mysqld"],
            ["SSH", "sshd"]
        ]
    };
}

and init that calling the function

loadConfig();

After that, I run my check on daemons.

function getDaemonStatus( )
{
    for(var i=0; i<config.daemons.length; i++)
    {

        child.exec( 'ps ax -o \'%c %P\' | awk \'{if (($2 == 1) && ($1 == "\'' +
            config.daemons[i][1] + '\'")) print $0}\'',
            function( error, stdout, stderr )
        {

            console.log(config.daemons[i]);
        });
    }
}

The response I get is:

undefined
undefined
undefined

I don’t really want to use a GLOBAL variable, so can you guys think of another way to solve my problem?

Thanks! =]

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T16:03:00+00:00Added an answer on May 26, 2026 at 4:03 pm

    This is a gotcha that lots of people run into because of the asynchronous ordering of execution.

    Your for loop will look from 0-3, and then exit when ‘i’ is four, obviously. The tough part to remember here is that your callback for exec won’t run immediately. In only runs once the process has started, and by the time that happens, the for loop will be done.

    That means that essentially, all three times that your callback function is running, you are essentially doing this:

    console.log(config.daemons[4]);
    

    That’s why it prints ‘undefined’.

    You need to capture the ‘i’ value in a new scope, by wrapping the loop contents in an anonymous, self-executing function.

    function getDaemonStatus( ) {
        for(var i=0; i<config.daemons.length; i++) {
            (function(i) {
    
                 child.exec( 'ps ax -o \'%c %P\' | awk \'{if (($2 == 1) && ($1 == "\'' +
                    config.daemons[i][1] + '\'")) print $0}\'',
                    function( error, stdout, stderr ) {
    
                    console.log(config.daemons[i]);
                });
    
            })(i);
        }
    }
    

    Also, I see that your function is called ‘getDaemonStatus’. Just remember that, since that exec callback is asyncronous, that also means that you can’t collect the results of each callback, and then return them from the getDaemonStatus. Instead, you will need to pass a your own callback, and call the it from inside your exec callback.

    Updated

    Note though, the easiest way to have a scope per-iteration is to use forEach, e.g.

    function getDaemonStatus( ) {
        config.daemons.forEach(function(daemon, i){
             child.exec( 'ps ax -o \'%c %P\' | awk \'{if (($2 == 1) && ($1 == "\'' +
                daemon[1] + '\'")) print $0}\'',
                function( error, stdout, stderr ) {
    
                console.log(daemon);
            });
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to create an if statement in PHP that prevents a single post
Basically, what I'm trying to create is a page of div tags, each has
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.