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

Only Get Stderr from Exec #371

Closed
360disrupt opened this issue Feb 23, 2016 · 13 comments
Closed

Only Get Stderr from Exec #371

360disrupt opened this issue Feb 23, 2016 · 13 comments
Labels
exec Issues specific to the shell.exec() API fix Bug/defect, or a fix for such a problem help wanted

Comments

@360disrupt
Copy link

I have the opposite problem to:
#92

On my local machine with nodemon everything works fine. On the server exec only gives me stderr and the code (which is 0 which equals no error).

I'm using node v.5.0.0. And it is started with pm2 on the server.

I actually don't know if this is a bug or maybe s.th. with the server config.

@nfischer
Copy link
Member

Hmmm... this seems like a strange issue. I don't think this is a bug with shelljs, since it seems to work on your local machine. Are you checking the .stdout attribute? .output is deprecated (and will be removed soon).

@360disrupt
Copy link
Author

The coffee gets compiled first by gulp before shelljs runs.

executeCommands = (commands, callback)->
  commandsToExecute = ""

  for command in commands
    commandsToExecute += "#{command} && "

  commandsToExecute += "echo \"finished\""

  shell.exec commandsToExecute, (code, stdout, stderr) ->
    logger.info "Exit code: #{code}", stdout
    logger.warn "Program stderr:", stderr
    err = if stderr? && stderr != "" then stderr else null
    return callback err, stdout, code
  • commands is an array of shell commands
  • callback is my callback to express

e.g. a route:

  app.get('/mongodump', routesService.loggedIn, routesService.cleanParams, (req, res) ->
    shellService.mongoDump req.user, (err, out, code) ->
      return res.json({err: err}) if err?
      return res.json({"out": out, "code": code})
  )

The weird thing is,t hat it makes a complete dump, there is no file missing. The rest of the message is pretty much the same and does not really indicate an error.I just noticed now, that there is the echo finished missing on the server. Could this be related to chaining the commands with &&, then I would rather use async.eachSeries to execute the commands.

If you tell me, that this is not related to shelljs I would transfer this issue to Stackoverflow.

@360disrupt
Copy link
Author

PS:
Same thing with async instead of chaining:

executeCommands = (commands, callback)->
  commandEchoFinished = "echo \"finished\""
  commands.push({"name": "commandEchoFinished", "command": commandEchoFinished})
  results = []
  async.eachSeries commands,
    (command, next)->
      shell.exec command.command, (code, stdout, stderr) ->
        logger.info "Command: #{command.name}, exited with code: #{code}", stdout
        logger.warn "Command: #{command.name}", stderr
        err = if stderr? && stderr != "" then stderr else null
        return next err if err?
        results.push({"command":command.name, "code":code, "stdout":stdout})
        return next null
    (err)->
      return callback err if err?
      return callback null, results

@nfischer
Copy link
Member

@360disrupt What if you chain the statements with ; instead of &&? Or what if you put echo 'Hello world' as the first command? That would check if it's because of a command failing toward the end.

@jeroenransijn
Copy link

I am having a similar issue, what is your environment. I am currently using Electron with ShellJS. Also I am Webpack, no idea if this is relevant.

@360disrupt
Copy link
Author

I can't really say what it was. It works now, I guess the provider of the server may have changed something. My environment is nodejs 5.0.0.

@nfischer
Copy link
Member

@jeroenransijn @360disrupt if either of you have the time to help figure out the bug, that would be much appreciated. I suspect it's due to differences in your environment (we list the supported nodejs and iojs versions in the README, anything outside of that we'll need help supporting).

Thanks!

@nfischer nfischer added help wanted fix Bug/defect, or a fix for such a problem labels Apr 11, 2016
@jeroenransijn
Copy link

@nfischer I was actually not well researched on what I was trying to do. Exec, even with the async option, waits for the process to finish and then gives you back the result of your command. I ended up using require('child_process').spawn() straight from node, which gives you a stream. I was wondering why there is no alternative in ShellJS?

@nfischer
Copy link
Member

It returns the child process, so you can use that to access stdout or stderr.

I think this should work:

var c = exec('cmd', {async: true});
c.stdout.on('data', function(stdout) {
  // do something with this chunk of data
});

@360disrupt
Copy link
Author

I will use shelljs in another project soon. If I see the problem again I can investigate. Currently I'm not able to look into it because it disappeared.

@nfischer
Copy link
Member

I will use shelljs in another project soon. If I see the problem again I can investigate. Currently I'm not able to look into it because it disappeared.

@360disrupt if you can figure out what the issue is, please ping this thread again (or open a new issue). Thanks!

@nfischer
Copy link
Member

@360disrupt @jeroenransijn were either of you able to identify the source of the issue?

@nfischer nfischer added the exec Issues specific to the shell.exec() API label Feb 7, 2018
@nfischer
Copy link
Member

nfischer commented Feb 7, 2018

We did a big exec refactor, so I'm assuming this is no longer relevant. Holler if this issue comes back.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
exec Issues specific to the shell.exec() API fix Bug/defect, or a fix for such a problem help wanted
Projects
None yet
Development

No branches or pull requests

3 participants