PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, September 14, 2022

[FIXED] How to declare a global array within an exec in javascript

 September 14, 2022     arrays, exec, global, javascript, linux     No comments   

Issue

I'm writing a js script to locate all pid files on a server, extract the pids and then run ' ps -fp {pid} ' on each of it and extract the memory usages.

This is what I've got so far:

var exec = require('child_process').exec;

exec('(cd ~/; find . -type f -name "*.pid" )', (error, stdout, stderr) => {
    if (error) {
      return 1
    }
    var fileps = stdout.toString().split('\n');
    fileps.pop()  
    var pidsarr = new Array;
    
    function getpid(path) {
    var command = `(cd ~/; cat ${path} )`;
    exec(command, (error, stdout, stderr) => {
        if (error) {
          return 1
        }
        pid = parseInt(stdout.toString());
        pidsarr.push(pid);
    });
    }

    fileps.forEach(getpid);    
    console.log(pidsarr); 
});

The issue is pidsarr is not getting updated, probably since it's not declared global or something to do with exec not allowing to write to external objects.

Any way to get my pidsarr array to get appended each run?


Solution

pidsarr is appended to for each run. However, you just schedule the runs in the background and don't wait for them to finish, so you don't see the final value.

You can verify this with a setTimeout(() => console.log(pidsarr), 1000);

To make it wait, you can either rewrite in terms of execSync, or have getpid return a Promise, then wait for all the promises to resolve:

var exec = require('child_process').exec;

exec('(cd ~/;  find . -type f -name "*.pid")', (error, stdout, stderr) => {
    if (error) {
      return 1
    }
    var fileps = stdout.toString().split('\n');
    fileps.pop()
    var pidsarr = new Array();

   function getpid(path) {
      var command = `(cd ~/; cat ${path} )`;
      return new Promise((resolve, fail) => {
        exec(command, (error, stdout, stderr) => {
          if (error) {
            fail();
          }
          pid = parseInt(stdout.toString());
          pidsarr.push(pid);
          resolve();
        });
      })
    }

    Promise.all(fileps.map(getpid)).then(() => console.log(pidsarr));
});

Note that it's bad practice to invoke find and cat like this. It's faster, more robust, and more portable to use glob and fs to find and read files without external tools.



Answered By - that other guy
Answer Checked By - Mildred Charles (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing