Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can not getting complete stdout #8807

Closed
nmanumr opened this issue Sep 27, 2016 · 11 comments
Closed

Can not getting complete stdout #8807

nmanumr opened this issue Sep 27, 2016 · 11 comments
Labels
child_process Issues and PRs related to the child_process subsystem. windows Issues and PRs related to the Windows platform.

Comments

@nmanumr
Copy link

nmanumr commented Sep 27, 2016

Version: v4.4.7
Platform: windows 10 (64 bit)

const spawn = require('child_process').spawn;
  const ls = spawn("D:\\Documents\\Nauman Umer\\New folder\\electron-quick-start\\PC-BASIC\\a.bat", []);

  ls.stdout.on('data', (data) => {
    console.log(`stdout: ${data}`);
  });

  ls.stderr.on('data', (data) => {
    console.log(`stderr: ${data}`);
  });

  ls.on('close', (code) => {
    console.log(`child process exited with code ${code}`);
  });

a.bat

"D:\Documents\Nauman Umer\New folder\electron-quick-start\PC-BASIC\pcbasic.com" --load="art.bas" --convert=A

its ouput when executed from command line:


D:\Documents\Nauman Umer\New folder\electron-quick-start\PC-BASIC>"D:\Documents\Nauman Umer\New folder\electron-quick-start\PC-BASIC\pcbasic.com" --load="art.bas" --convert=A
940 REM The IBM Personal Computer Art
950 REM Version 1.10 (C)Copyright IBM Corp 1981, 1982
960 REM Licensed Material - Program Property of IBM
970 REM Author - Glenn Stuart Dardick
975 DEF SEG:  POKE 106,0
980 SAMPLES$ = "NO"
990 GOTO 1010
[AND SO ON]

pcbasic is written in python and using sys.stdout.write() instead of print.

but when I run this bat file with from my electron app using to about javascript code it results

stdout:
D:\Documents\Nauman Umer\New folder\electron-quick-start>"D:\Documents\Nauman Umer\New folder\electron-quick-start\PC-BASIC\pcbasic.com" --load="art.bas" --convert=A
[PROGRAM OUTPUT MISSING]
child process exited with code 0

I also tried spawn, exec and execFile but getting same issue. and i also tried to execute pcbasic directly from program instead of from batch file but getting same problem. when i tried to redirect pcbasic output to file i noticed that a file is created but it is empty.
I tried following to redirect:

  • in bat file
"D:\Documents\Nauman Umer\New folder\electron-quick-start\PC-BASIC\pcbasic.com" --load="art.bas" --convert=A > a.txt
  • in electron app
spawn(`"D:\\Documents\\Nauman Umer\\New folder\\electron-quick-start\\PC-BASIC\\a.bat" > a.bat`, []);
@mscdex mscdex added the child_process Issues and PRs related to the child_process subsystem. label Sep 27, 2016
@grivescorbett
Copy link

I think this is happening to me. In this example I see output in os x but not in ubuntu/docker. Using node 6.7.0

test.js

const childprocess = require('child_process');

const child = childprocess.spawn('./script.sh', []);

child.stdout.on('data', buffer => {
  console.log(buffer.toString());
})

script.sh

echo "abc" > /dev/stdout

@Trott
Copy link
Member

Trott commented Sep 30, 2016

/cc @Fishrock123

@Fishrock123
Copy link
Contributor

Are either of you calling process.exit() in your code or does a dependency call it?

@nmanumr
Copy link
Author

nmanumr commented Oct 1, 2016

@Fishrock123 the process is exited automatically after execution

@Fishrock123 Fishrock123 added windows Issues and PRs related to the Windows platform. and removed windows Issues and PRs related to the Windows platform. labels Oct 3, 2016
@Fishrock123
Copy link
Contributor

maybe @bnoordhuis or @cjihrig?

@Trott
Copy link
Member

Trott commented Jul 15, 2017

@nodejs/platform-windows

@refack
Copy link
Contributor

refack commented Jul 15, 2017

I'll give this a look but if the issue in the OP is about Electron it should go in that repo, since Electron handles stdio differently because it is a "Windows GUI" application and not "Windows Console".
Ref: #556

@refack
Copy link
Contributor

refack commented Jul 17, 2017

@naumanumer I just tried the attached app with Electron@1.7.4.0 that uses node@7.9.0, and it seems to work:
image
the code:

<!DOCTYPE html>
<html>
  <head>
    <title>Hello World!</title>
  </head>
  <body>
    <h1>stdout</h1>
    <pre id=stdout></pre>
    <h1>stderr</h1>
    <pre id=stderr></pre>
    <h1>stdout</h1>
    <pre id=exitline></pre>
  </body>
  <script>
  const spawn = require('child_process').spawn;
  const ls = spawn(`${__dirname}/ls.bat`, []);

  ls.stdout.on('data', (data) => {
    document.getElementById('stdout').innerHTML += data.toString().replace(/</g, '&lt;');
  });

  ls.stderr.on('data', (data) => {
    document.getElementById('stderr').innerHTML += data;
  });

  ls.on('close', (code) => {
    document.getElementById('exitline').innerHTML += `child process exited with code ${code}\n`;
  });
  </script>
</html>

with ls.bat:

dir *.*

electronstdout8807.zip

@bzoz
Copy link
Contributor

bzoz commented Jul 21, 2017

Also, spawn needs {shell: true} to reliably call .bat files. I'll close this, you can reopen the issue if it persists.

@bzoz bzoz closed this as completed Jul 21, 2017
@sam-github
Copy link
Contributor

@bzoz I don't think spawn needs a shell to call .bat files. It does for .cmd files (which is what npm uses to wrap node CLIs), but .bat, .com, and .exe are considered executable and can be directly spawned unless I very much misremember.

Or perhaps the key is "reliably", is there something unreliable about .bat files run without a shell?

@refack
Copy link
Contributor

refack commented Jul 21, 2017

Or perhaps the key is "reliably", is there something unreliable about .bat files run without a shell?

I think "reliably" is indeed the key point.
When {shell: true} node controls the emergent cmd.exe process. When {shell: false} the OS decides which program to launch to run the .bat (BTW by default .bat and .cmd are equivalent, but that's configurable), and how uv catches that process, I don't know.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
child_process Issues and PRs related to the child_process subsystem. windows Issues and PRs related to the Windows platform.
Projects
None yet
Development

No branches or pull requests

9 participants