question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

container.execute long running command stdout only returns start of output

See original GitHub issue

It seems when a command takes a while to run, the stdout only returns the first part of the output. If I use subprocess to run the same command via the lxc cli, then I get the full output. For example:

#!/usr/bin/env python

from pylxd import Client
import subprocess
import time

#args = ['ls', '-la']
args = ['ping', '-c', '4', 'localhost']

client = Client()
print('Create container')
#config = {'name': 'apphost', 'source': {'type': 'image', 'fingerprint': '012d2462c11a'}} #centos/7
config = {'name': 'apphost', 'source': {'type': 'image', 'fingerprint': '31235ca07a33'}} #ubuntu/18.04
container = client.containers.create(config, wait=True)
print('Start container')
container.start(wait=True)
print('==========\npylxd container.execute')
(code, stdout, stderr) = container.execute(args, environment={'LANG': 'en_AU.UTF-8'})
print('code:', code)
print('stdout:', stdout)
print('stderr:', stderr)
print('==========\nsubprocess.run')
proc = subprocess.run(['lxc', 'exec', 'apphost', '--env', 'LANG=en_AU.UTF-8', '--'] + args,
        stdout=subprocess.PIPE,
        stderr=subprocess.PIPE,
)
print('code:', proc.returncode)
print('stdout:', proc.stdout.decode())
print('stderr:', proc.stderr.decode())
container.stop(wait=True)
container.delete()

output:

Create container                            
Start container                                                                      
==========          
pylxd container.execute 
code: 0                 
stdout: PING localhost(localhost (::1)) 56 data bytes
64 bytes from localhost (::1): icmp_seq=1 ttl=64 time=0.015 ms                             
                               
stderr:                        
==========
subprocess.run                 
code: 0                               
stdout: PING localhost(localhost (::1)) 56 data bytes
64 bytes from localhost (::1): icmp_seq=1 ttl=64 time=0.014 ms
64 bytes from localhost (::1): icmp_seq=2 ttl=64 time=0.034 ms
64 bytes from localhost (::1): icmp_seq=3 ttl=64 time=0.047 ms
64 bytes from localhost (::1): icmp_seq=4 ttl=64 time=0.034 ms
      
--- localhost ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3049ms
rtt min/avg/max/mdev = 0.014/0.032/0.047/0.012 ms
                                               
stderr:                                         

For quick commands like ls -la / the full output is returned.

Environment:

  • Ubuntu 18.04.2
  • Python 3.6.7
  • lxd 3.0.3

Pip installed:

  • pylxd (2.2.9)
  • ws4py (0.5.1)

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
ajkavanaghcommented, May 17, 2019

@kimfaint Hi, I found the error and fixed it. It took a bit of testing (and staring at the lxd go client code) to work out what was going on. This version seems to work, but there’s a possibility that very slow network connections might mix up the reception of various packets and miss stdout or stderr data. Actually doing it ‘properly’ and waiting for the close from the LXD server daemon adds a 30-60 seconds wait to the end of the message. LXD sends an empty string to indicate the end of it’s stream, and we look for that.

Anyway, this is much better than it was. If you can test from master that would be great, and then I’ll see to get a new version released on PyPi as this is a pretty bad error! Thanks.

0reactions
ajkavanaghcommented, May 20, 2019

@kimfaint great stuff; I’ll get a release sorted out. Thanks.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to get docker exec stdout to be as verbose as running ...
It simply runs the command, but nothing is outputted to my terminal window. However, if I actually go into the container and run...
Read more >
docker run - Docker Documentation
The docker run command first creates a writeable container layer over the specified image, and then starts it using the specified command.
Read more >
How to redirect shell command output | Enable Sysadmin
There are multiple ways to redirect output from shell scripts and commands. 1. Redirect STDOUT. For the following examples, I will use this ......
Read more >
How to Live Tail Docker Logs - Papertrail
This command prints a list of running containers. You'll see their IDs, used images, start commands, how long the containers have been running,...
Read more >
os/exec - Go Packages
Run starts the specified command and waits for it to complete. The returned error is nil if the command runs, has no problems...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found