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

How Can I read the taskExec output ? #382

Closed
borgogelli opened this issue Jul 20, 2016 · 9 comments
Closed

How Can I read the taskExec output ? #382

borgogelli opened this issue Jul 20, 2016 · 9 comments

Comments

@borgogelli
Copy link

borgogelli commented Jul 20, 2016

Hi

some task, call a system command which produces some outputs.
Robo can prints out them on video but how can I store it in a var ?

For example

 $result = $this->taskExec('java -jar ' . $my_jar)
        ->background()
        ->run();

Can I extract the command output from the $result object ?

Thank you

Andrea

@Sweetchuck
Copy link
Contributor

@borgogelli
Copy link
Author

borgogelli commented Jul 22, 2016

Hi Sweetchuck ,
getMessage() sometimes returns an empty string like in following snippet:

$result = $this->taskSvnStack($svn_user, $svn_password)
        ->dir($path)
        ->exec("info")
        ->printed(true)
        ->run();

@Sweetchuck
Copy link
Contributor

It gives a different result on success and on fail.
https://github.com/consolidation-org/Robo/blob/141bc9976e73201b93e9ee07c950951f239b278b/src/Task/CommandStack.php#L65

On success there is no information about the StdOut

@borgogelli
Copy link
Author

Well, so how can I read the StdOut when task end successfully ? See the above example, where I'd like to read the output of the "svn info" command. I know that a task has a logger object linked. Could it be useful to read the StdOut ?

@greg-1-anderson
Copy link
Member

Do you need access to stdout for tasks that perform an operation?

In your first example, you are using ->background() to run a Java server in the background. A Robo execTask is not a good way to monitor the output from a long-running background task. Use a log file, or run the process via the Symfony process component directly.

In the second example, a Robo taskExec is not a good way to obtain information about the system, e.g. when building your task objects. Instead, you should use the php exec function, or the Symfony Process component to call things such as svn info.

Use taskExec for commands that change the state of the system. When implementing a task that needs to exec a tool, then it is preferable to use ExecCommand or extend your class from CommandStack.

@borgogelli
Copy link
Author

borgogelli commented Aug 9, 2016

Thanks a lot Greg for clearing up the usage of "taskExec".
The confusion arises from the following example in the getting-start guide:
$this->taskExec('ls')->args($args )->run() ; (here I get information and I don't change the state of the system)
I think that your clarification will also be useful to other users .
Regards from Italy

@greg-1-anderson
Copy link
Member

Good point -- we should change that example.

justinlevi added a commit to justinlevi/Robo that referenced this issue Nov 27, 2018
Update the alternative approach to using returned results to be more inline with the suggestions proposed here:
consolidation#382

Using the Symfony Process component for returning simple system info.
@geek-merlin
Copy link

I had that problem with current robo / openeuropa/task-runner, and the search engine directed me here.

What made me succeed was ->printOutput(false)like so:

    $hash = $this->taskExec('git rev-parse HEAD')
      ->printOutput(false)
      ->run()->stopOnFail()->getMessage();

@dgrothaus-mc
Copy link

    $hash = $this->taskExec('git rev-parse HEAD')
      ->printOutput(false)
      ->run()->stopOnFail()->getMessage();

You are the MVP. That helped a lot. Thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants