rundeck-plugins/docker

Shell output is not in realtime

ryancurrah opened this issue · 4 comments

When running a docker exec or docker run the output is not printed to stdout in realtime. This makes it hard to debug a hanging command. I propose the ability to print stdout and stderr from the subprocess command in realtime.

This would require a while loop to poll() the status of the command execution and print the results to stdout instead of the wait() function.

@ryancurrah I wonder if it is due to buffering.

This is because we are waiting for the command to finish executing then printing the result to stdout see line 101.

I tried some code just now and it worked. As our command was running and printing lines it was appearing in the Rundeck console at the same time instead of it all appearing once the command finished. Below is the snippet....

p = Popen(
    shlex.split(command_string),
    shell=False,
    stdout=PIPE,
    stderr=PIPE
)

# Print lines to stdout while waiting for command to finish
while True:
    line = p.stdout.readline().rstrip()
    if line:
        print line
    if p.poll() is not None:
        break

exitcode = p.poll()

if exitcode != 0:
    log.error('Command execution failed with exit code: %s' % str(exitcode))

@ryancurrah Thanks for that code example. If it works well, create a pull request.

That was the plan. I'm going to test for a bit first, if it works well I will open a PR and close this issue.