I want to spawn top
with a few arguments to get the current cpu load & usage.
If I type the full command on my ssh session top -bn1 | grep "Cpu(s)\|top -"
, I get the complete and working response.
But how is the correct way to spawn this command with execFile
?
This is what I want to do:
import childProcess from 'child_process'
import util from 'util'
const execFile = util.promisify(childProcess.execFile)
async function getData() {
// Not working
const command = 'top -bn1 | grep "Cpu(s)\|top -"'
const args = []
// Also tried this
const command = 'top'
const args = ['-bn1 | grep "Cpu(s)\|top -"']
const { stdout } = await execFile(command, args, { maxBuffer: 1000 * 1000 * 10 })
console.log(stdout)
}
getData()
But the spawn will fail with the following error:
Error: spawn top -bn1 | grep "Cpu(s)|top -" ENOENT
at Process.ChildProcess._handle.onexit (node:internal/child_process:282:19)
at onErrorNT (node:internal/child_process:480:16)
at processTicksAndRejections (node:internal/process/task_queues:83:21) {
errno: -2,
code: 'ENOENT',
syscall: 'spawn top -bn1 | grep "Cpu(s)|top -"',
path: 'top -bn1 | grep "Cpu(s)|top -"',
spawnargs: [],
cmd: 'top -bn1 | grep "Cpu(s)|top -"',
stdout: '',
stderr: ''
}
Via Active questions tagged javascript - Stack Overflow https://ift.tt/2FdjaAW
Comments
Post a Comment