2

I'm executing a python script from within node using execSync like this:

execSync('python myScript.py -o "/dev/stdout"');

My script will send the result to the file specified at -o in this case /dev/stdout.

I get the error message "No such device or address: '/dev/stdout'".

If I execute the command manually from the terminal it runs fine, but not when started via node.

I'm running Ubuntu 12.04.

asantacreu
  • 192
  • 1
  • 3
  • 18
Drahcir
  • 11,772
  • 24
  • 86
  • 128
  • Is it interpreting the quote marks as literally part of the filename? Try removing the double quotes. – John Gordon Oct 27 '16 at 15:25
  • @JohnGordon I tried with and without the quotes – Drahcir Oct 27 '16 at 15:26
  • This [thread](http://unix.stackexchange.com/q/36403/134816) might be useful to you. – CAB Oct 27 '16 at 15:29
  • Are you getting a Python traceback or is that Ubuntu throwing that at you? Does your script write to the file or are you intending to pipe the output through this command? – sytech Oct 27 '16 at 15:34
  • @sytech Trackback – Drahcir Oct 27 '16 at 15:34
  • Is your python script trying to do something like `open("/dev/stdout")` or are you writing to or changing `sys.stdout`? It may be helpful to post the portion of the Python code that produces the output based on the -o flag. – sytech Oct 27 '16 at 15:47
  • @sytech it uses `open`: `with open(output_filename, 'w') as f:` – Drahcir Oct 27 '16 at 15:54
  • @Drahcir this is your problem, I believe. Instead of having the Python script determine the outfile location, you should write to `sys.stdout` and use `execSync`'s options to determine the location of the child process's stdout in a threadsafe manner. – sytech Oct 27 '16 at 16:05
  • This question is too broad and I have voted to close. Thank you everyone for your help but my problem has not been resolved. I have opened a new question (http://stackoverflow.com/questions/40301841/cannot-create-dev-stdout-no-such-device-or-address). – Drahcir Oct 28 '16 at 09:16

1 Answers1

0

When you run the command in a terminal, /dev/stdout redirects to the terminal stdout. When you spawn the python process with execSync there's no place for stdout to be redirected to.

If you read a bit further down the execsync page, it describes how to use a child process' stdout

CAB
  • 1,106
  • 9
  • 23
  • Thanks for your answer but it doesn't solve the problem. I had read this already, it describes how to forward the output from the child process's stdout to the parent's. – Drahcir Oct 27 '16 at 15:42
  • 1
    @Drahcir To be clear, it creates a pipe that the parent can read from. The example shown redirects the data read from the pipe directly to a file, so you don't have to write code to do that. You could do anything with that output, including directing it to `sys.stdout`, which would be the parent's stdout. The question is, where do you want that output to go? – CAB Oct 27 '16 at 15:47
  • I want to do this: `JSON.parse(execSync('python ...etc'))` – Drahcir Oct 27 '16 at 15:50
  • 1
    @Drahcir I think it might be worthwhile to post a new question; 'How to read output of a pipe in NodeJs'. I'm not a node guy, but there should be a readable pipe, or readable stream construct. Essentially, create a readable pipe, hand that to the child process as stdout, hand it to JSON.parse as stdin. – CAB Oct 27 '16 at 15:57
  • execSync returns the value of the stdout stream, no need to read it manually, and the default value of the stdio stdout is "pipe" so it is already an empty stream. The problem is that the python script can't access the stream. – Drahcir Oct 27 '16 at 16:10
  • If that is the case, you don't need the `-o "/dev/stdout"` at all. You just need to make sure the python script is writing the data you want to its `stdout`. That would be `sys.stdout`, which is the default for `print`. Otherwise, use `sys.stdout.write(some_json)`. – CAB Oct 27 '16 at 16:22