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! =]
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.
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);
});
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With