since a while i am trying to reach something that doesn't work out for me so far.
With nodejs, i like to run a interactive sh-command and work with the sh-command output after the command has exited. i like to write a yieldable generator-function that wraps the running of the interactive shell command and returns the output of the shell command.
options: { stdio: 'inherit' }
So my question at all. Can someone post me an example of how to run a interactive shell command that can be wrapped in a yieldable generator function that returns the output of the shell command? i am open for new approaches.
I created a npm module which is available on github where you can fork it and contribute.
thx in advance.
child_process. exec() : spawns a shell and runs a command within that shell, passing the stdout and stderr to a callback function when complete. child_process. execFile() : similar to child_process. exec() except that it spawns the command directly without first spawning a shell by default.
Usually, Node. js allows single-threaded, non-blocking performance but running a single thread in a CPU cannot handle increasing workload hence the child_process module can be used to spawn child processes. The child processes communicate with each other using a built-in messaging system.
spawn() method launches a new process with a given command. This method returns streams (stdout & stderr) and it is generally used when the process returns large amount of data. Syntax: child_process. spawn(command[, args][, options])
Spawn is useful when you want to make a continuous data transfer in binary/encoding format — e.g. transferring a 1 Gigabyte video, image, or log file. Fork is useful when you want to send individual messages — e.g. JSON or XML data messages.
I found the following which works on v5.4.1 . In the docs NodeJS Child Process it mentions the option encoding which has a default of 'buffer'. If you set this option to 'utf8', then instead of a buffer you get a string back with the results. You can get a string back from spawnSync because it is synchronous and blocks execution until the command completes. Here's a working example of a script which does an 'ls -l /usr' command and gets the output as a string object:
#!/usr/bin/env node
var cp = require('child_process');
var ls = cp.spawnSync('ls', ['-l', '/usr'], { encoding : 'utf8' });
// uncomment the following if you want to see everything returned by the spawnSync command
// console.log('ls: ' , ls);
console.log('stdout here: \n' + ls.stdout);
When you run it you get the following:
stdout here:
total 68
drwxr-xr-x 2 root root 36864 Jan 20 11:47 bin
drwxr-xr-x 2 root root 4096 Apr 10 2014 games
drwxr-xr-x 34 root root 4096 Jan 20 11:47 include
drwxr-xr-x 60 root root 4096 Jan 20 11:47 lib
drwxr-xr-x 10 root root 4096 Jan 4 20:54 local
drwxr-xr-x 2 root root 4096 Jan 6 01:30 sbin
drwxr-xr-x 110 root root 4096 Jan 20 11:47 share
drwxr-xr-x 6 root root 4096 Jan 6 00:34 src
The docs tell you what you can get back on the object in addition to stdout . If you want to see all the properties on the return object, uncomment the console.log (Warning: there's a LOT of stuff :) ).
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