1

Uploading a file using cURL in a Node.js child_process spawn.

It seems to be working fine (the file uploads without fail), but the output streams to stderr instead of stdout.




JS is

curlOps=["-T", ThisJobRslts.DestPath, "-u", "userid@ftpdomain.com:password", "ftp://host.address.com"];
spawn=require("child_process").spawn("curl", curlOps);
spawn.stdout.on("data", (data) => {
    console.log("stdout ", data.toString("utf8"));
});
spawn.stderr.on("data", (data) => {
    console.log("stderr " + data.toString("utf8"));
});
spawn.on("close", (data) => {
    console.log("Closed", data);    
});




Console gets:

stderr   % Total    % Received % Xferd  Average Speed   Time    Time     Time  Cur
stderr rent
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
stderr 100   120    0     0  100   120      0     99  0:00:01  0:00:01 --:--:
stderr --    99
stderr 100   120    0     0  100   120      0     54  0:00:02  0:00:02 --:--:--  
stderr   54
stderr 100   120    0     0  100   120      0     37  0:00:03  0:00:03 --:--:--    37
stderr 100   120    0     0  100   120      0     28  0
stderr :00:04  0:00:04 --:--:--    28
stderr 100   120    0     0  100   120      0     23  0:00:05  0:00:
stderr 05 --:--:--    23
stderr 100   120    0     0  100   120      0     19  0:00:06  0:00:06 --:--:
stderr --     0
stderr 100   120    0     0  100   120      0     16  0:00
stderr :07  0:00:07 --:--:--     0
stderr 100   120    0     0  100   120      0     14  0:00:08  0:00:08 --:--:--     0
stderr 100   120    0     0  100   120      0     13  0:00:09  0:00:
stderr 09 --:--:--     0
stderr 100   120    0     0  100   120      0     11  0:00
stderr :10  0:00:10 --:--:--     0
stderr 100   161    0    41  100   120      3     11  0:00:10  0:00:10 --:--:-
stderr -     0
baza92
  • 344
  • 1
  • 10
WhatsYourFunction
  • 621
  • 1
  • 9
  • 25
  • I executed your code (I changed `curl` to `pwd` command and removed parameters) and everything is ok (output is displayed in stdout). Are you sure that curl command returns `0`? – baza92 Apr 02 '19 at 21:14
  • You can add something like: `spawn.on('exit', (code, signal) => {console.log('child process exited with code ' + code)});` for verification. – baza92 Apr 02 '19 at 21:16
  • 1
    After some reading looks like it is the problem with `curl`itself. As suggested here: https://stackoverflow.com/questions/6935006/making-curl-send-errors-to-stderr-and-everything-else-to-stdout you can add flags `--fail --silent --show-error` to redirect errors to `stderr` and rest of output to `stdout` – baza92 Apr 02 '19 at 21:24
  • baza92 - thanks for the link. Very helpful. It does look like a cURL issue. The flags didn't exactly help as far as I can tell. They simply silence the output and allow you to save it up until the end. The goal would be to stream the output and _accurately_ distinguish stderr from stdout. Apparently there are a number of CLI tools that output to stderr for whatever reason. Will keep looking up on this. Thanks again. – WhatsYourFunction Apr 02 '19 at 22:24

0 Answers0