Is it possible to monitor terminal for specific output? - node.js

I am using NodeJS to run webots by command line and have redirected the stdout too the node terminal. My problem is that I want to trigger an event based on a console log. I tried redirecting the stdout of the command to another file, but this didn't seem to work.
This is the console output
INFO: sumo_example_two: Starting controller: python.exe -u sumo_example_two.py
INFO: sumo_supervisor: Starting controller: python.exe -u sumo_supervisor.py
robot2
INFO: sumo_example_one: Terminating.
INFO: sumo_example_two: Terminating.
INFO: sumo_supervisor: Terminating.
stdout:
I want to extract 'robot2'.

I have just tested and the following snippet works fine for me:
const { spawn } = require('child_process');
const ls = spawn('webots', ['--stdout']);
ls.stdout.on('data', (data) => {
console.log(`stdout: ${data}`);
// Process `data` as you prefer, something like
//
// if (data.includes('robot2')) {
// something()
// }
});

Related

Can't seem to figure out how to get the output of an process fired with spawn. stdout.on('data') not outputting for me

I am trying to setup some automation on a game server for the game Rust.
The game server itself is ran by running its executable file RustDedicated.exe with some arguments.
According to some googling and reading here on Stack Overflow I have made this script:
import config from "config";
import { ChildProcessWithoutNullStreams, spawn } from "child_process";
const GAMESERVERPATH: string = config.get("Environment.RustDedicatedPath");
const EXECUTABLE: string = config.get("Environment.ExecutableFile");
const GAMESERVERARGS: Array<string> = [
"-batchmode",
"+server.port", `${config.get("Server.port")}`,
"+server.level", `"${config.get("Server.level")}"`,
"+server.seed", `${config.get("Server.seed")}`,
"+server.worldsize", `${config.get("Server.worldsize")}`,
"+server.maxplayers", `${config.get("Server.maxplayers")}`,
"+server.hostname", `"${config.get("Server.hostname")}"`,
"+server.description", `"${config.get("Server.description")}"`,
"+server.headerimage", `"${config.get("Server.headerimage")}"`,
"+rcon.port", `${config.get("Rcon.port")}`,
"+rcon.password", `"${config.get("Rcon.password")}"`,
"+rcon.web", `${config.get("Rcon.web")}`
];
const gameServerProc : ChildProcessWithoutNullStreams = spawn(
GAMESERVERPATH+EXECUTABLE,
GAMESERVERARGS,
{
cwd: GAMESERVERPATH,
shell: true,
}
);
gameServerProc.stdout.on("data", (data) => {
console.log(`stdout:${data.toString()}`);
});
gameServerProc.stderr.on("data", (data) => {
console.log(`stderr:${data.toString()}`);
});
gameServerProc.on("error", (err) => {
console.log(`error:${err.message}`);
});
What is happening is that i can see the output of the executable in the terminal window, and the server is firing without errors, but it seems that stdout is not firing the on('data') event.
I never see stdout:.
See the screenshot below where i have Code open, the output is on the bottom right.
Why is my script failing to get the on('data') firing when the executable outputs?
This issue had nothing to do with the code, but rather the game engine Unity. An extra argument -logFile - had to be present for the executable to output to stdout.

How can I handle an input prompt with node when executing a command via child_process?

For context, I'm on a Mac and I'm trying to script a 1Password CLI signin via their command-line tool. I'm trying to programmatically signing using a command that looks like:
op signin <signinaddress> <emailaddress> <secretkey> --output=raw
and I've tried with/without the --output=raw argument, but every time I simply get an error that looks like
[LOG] 2019/06/04 00:57:45 (ERROR) operation not supported on socket
child process exited with code 1
My initial hunch was that it had something to do with the command executions prompt displaying this special key character in the following image:
The relevant code is written in TypeScript and looks like this:
import { spawn } from 'child_process'
// ends up being `op signin <signinaddress> <emailaddress> <secretkey>`
const op = spawn(opExecutable, args);
let result: string | null = null
op.on('message', (message, sendHandle) => {
console.log('message', message, sendHandle)
});
op.stdout.on('data', (data) => {
console.log(`stdout: ${data}`);
if (data && typeof data.toString === 'function') {
result = data.toString()
}
});
op.on('close', (code, ...args) => {
console.log(`child process exited with code ${code}`, args);
});
Eventually, I'd like to run on all platforms and be able pass in stdin for the master password required to sign in, but I'm trying to figure out why my node app is crashing first :)
Apparently I was pretty close to a solution by using spawn, but I needed to specify configuration for stdio. Here's an example snippet of how I used spawn that worked for me:
const proc = spawn(
cmd, // the command you want to run, in my case `op`
args, // arguments you want to use with the above cmd `signin`, etc.
{
stdio: [
'inherit', // stdin: changed from the default `pipe`
'pipe', // stdout
'inherit' // stderr: changed from the default `pipe`
]
});

Callback when starting Android emulator with shell command via node

I am opening an android emulator with node via a shell script:
var process = require('child_process');
process.exec('~/Library/Android/sdk/tools/emulator -avd Nexus_5_API_21_x86', processed);
function processed(data){
console.log('processed called', data, data.toString());
}
I need to be able to detect when the emulator has finished loading so I can initiate a screen unlock and then launch the browser to a specified url (~/Library/Android/sdk/platform-tools/adb shell input keyevent 82 and ~/Library/Android/sdk/platform-tools/adb shell am start -a android.intent.action.VIEW -d http://www.stackoverflow.com)
However, when I launch the emulator I don't appear to get anything back and the process stays engaged with the emulator. When shutting the process down (ctrl+c) the emulator is closed along with it. (This is the same behaviour as running the shell command directly in the terminal)
Is it possible to know when the emulator has opened and loaded?
How can I execute additional commands when the process continues to
run?
I solved it like a boss.
I was able to set a timer to check once a second if the bootanimation had stopped. If it has, we know the emulator is open and booted.
var process = require('child_process');
process.exec('~/Library/Android/sdk/tools/emulator -avd Nexus_5_API_21_x86');
function isEmulatorBooted(){
process.exec('~/Library/Android/sdk/platform-tools/adb shell getprop init.svc.bootanim', function(error, stdout, stderr){
if (stdout.toString().indexOf("stopped")>-1){
clearInterval(bootChecker);
emulatorIsBooted();
} else {
console.log('we are still loading');
}
});
}
function emulatorIsBooted(){
//unlock the device
process.exec('~/Library/Android/sdk/platform-tools/adb shell input keyevent 82');
//gotourl
process.exec('~/Library/Android/sdk/platform-tools/adb shell am start -a android.intent.action.VIEW -d http://192.168.10.126:9876/');
}
bootChecker = setInterval(function(){
isEmulatorBooted();
},1000);
In case anyone else is looking for this, made an alternate version of Fraser's script - which also starts the actual emulator in a background process.
#!/usr/bin/env node
const process = require('child_process');
/* Get last emulator in list of emulators */
process.exec("emulator -list-avds|sed '$!d'", (_, stdout) => {
console.log('[android emulator] Booting')
/* Start emulator */
process.exec(`nohup emulator -avd ${stdout.replace('\n', '')} >/dev/null 2>&1 &`)
/* Wait for emulator to boot */
const waitUntilBootedThen = completion => {
process.exec('adb shell getprop init.svc.bootanim', (_, stdout) => {
if (stdout.replace('\n', '') !== 'stopped') {
setTimeout(() => waitUntilBootedThen(completion), 250)
} else {
completion()
}
})
}
/* When emulator is booted up */
waitUntilBootedThen(() => {
console.log('[android emulator] Done')
})
})

Use child_process.execSync but keep output in console

I'd like to use the execSync method which was added in NodeJS 0.12 but still have the output in the console window from which i ran the Node script.
E.g. if I run a NodeJS script which has the following line I'd like to see the full output of the rsync command "live" inside the console:
require('child_process').execSync('rsync -avAXz --info=progress2 "/src" "/dest"');
I understand that execSync returns the ouput of the command and that I could print that to the console after execution but this way I don't have "live" output...
You can pass the parent´s stdio to the child process if that´s what you want:
require('child_process').execSync(
'rsync -avAXz --info=progress2 "/src" "/dest"',
{stdio: 'inherit'}
);
You can simply use .toString().
var result = require('child_process').execSync('rsync -avAXz --info=progress2 "/src" "/dest"').toString();
console.log(result);
Edit: Looking back on this, I've realised that it doesn't actually answer the specific question because it doesn't show the output to you 'live' — only once the command has finished running.
However, I'm leaving this answer here because I know quite a few people come across this question just looking for how to print the result of the command after execution.
Unless you redirect stdout and stderr as the accepted answer suggests, this is not possible with execSync or spawnSync. Without redirecting stdout and stderr those commands only return stdout and stderr when the command is completed.
To do this without redirecting stdout and stderr, you are going to need to use spawn to do this but it's pretty straight forward:
var spawn = require('child_process').spawn;
//kick off process of listing files
var child = spawn('ls', ['-l', '/']);
//spit stdout to screen
child.stdout.on('data', function (data) { process.stdout.write(data.toString()); });
//spit stderr to screen
child.stderr.on('data', function (data) { process.stdout.write(data.toString()); });
child.on('close', function (code) {
console.log("Finished with code " + code);
});
I used an ls command that recursively lists files so that you can test it quickly. Spawn takes as first argument the executable name you are trying to run and as it's second argument it takes an array of strings representing each parameter you want to pass to that executable.
However, if you are set on using execSync and can't redirect stdout or stderr for some reason, you can open up another terminal like xterm and pass it a command like so:
var execSync = require('child_process').execSync;
execSync("xterm -title RecursiveFileListing -e ls -latkR /");
This will allow you to see what your command is doing in the new terminal but still have the synchronous call.
Simply:
try {
const cmd = 'git rev-parse --is-inside-work-tree';
execSync(cmd).toString();
} catch (error) {
console.log(`Status Code: ${error.status} with '${error.message}'`;
}
Ref: https://stackoverflow.com/a/43077917/104085
// nodejs
var execSync = require('child_process').execSync;
// typescript
const { execSync } = require("child_process");
try {
const cmd = 'git rev-parse --is-inside-work-tree';
execSync(cmd).toString();
} catch (error) {
error.status; // 0 : successful exit, but here in exception it has to be greater than 0
error.message; // Holds the message you typically want.
error.stderr; // Holds the stderr output. Use `.toString()`.
error.stdout; // Holds the stdout output. Use `.toString()`.
}
When command runs successful:
Add {"encoding": "utf8"} in options.
execSync(`pwd`, {
encoding: "utf8"
})

Node JS: Executing command lines and getting outputs asynchronously

How can I run a command line and get the outputs as soon as available to show them somewhere.
For example if a run ping command on a linux system, it will never stop, now is it possible to get the responses while the command is still processing ?
Or let's take apt-get install command, what if i want to show the progress of the installation as it is running ?
Actually i'm using this function to execute command line and get outputs, but the function will not return until the command line ends, so if i run a ping command it will never return!
var sys = require('sys'),
exec = require('child_process').exec;
function getOutput(command,callback){
exec(
command,
(
function(){
return function(err,data,stderr){
callback(data);
}
}
)(callback)
);
}
Try using spawn instead of exec, then you can tap into the stream and listen to the data and end events.
var process = require('child_process');
var cmd = process.spawn(command);
cmd.stdout.on('data', function(output){
console.log(output.toString()):
});
cmd.on('close', function(){
console.log('Finished');
});
//Error handling
cmd.stderr.on('data', function(err){
console.log(err);
});
See the Node.js documentation for spawn here: https://nodejs.org/api/child_process.html#child_process_child_process_spawn_command_args_options

Resources