21

I want to execute a command inside an existing lxc without going through the regular Linux init. lxc-execute command is for that I guess but I get the following error when I run this command on my existing test lxc.

sudo lxc-execute -n test -- service apache2 start 

I get following error:

lxc-execute: Failed to find an lxc-init
lxc-execute: invalid sequence number 1. expected 4
lxc-execute: failed to spawn 'test'
user52881
  • 211
  • 1
  • 2
  • 3

2 Answers2

21

Have you tried lxc-attach instead? According the documentation,

lxc-attach - start a process inside a running container.

Example to list all folders inside home of the container

lxc-attach -n myContainer -- ls -lh /home

If no process is defined, it creates a new shell inside the container.

Xavi Ivars
  • 311
  • 2
  • 5
  • The thing with `lxc-attach` is that you can't use it with `less`, e.g. `lxc-attach -n c1 -- dmesg | less`. The output is similar to what you get with `docker-compose exec ... | less` unless you pass `-T`. – x-yuri Jul 21 '21 at 15:30
1

lxc-execute is designed to create and run and lxc container and execute the command. So it can not be used to execute a command currently setup and started one.

Quotation from Serge E. Hallyn @ https://www.mail-archive.com/[email protected]/msg00134.html

Moving an existing process into an existing, populated namespace will likely never be possible.

You may try Ullrich Horlacher's lxc command but currently the page is out of order. Process is described in this post:

https://www.mail-archive.com/[email protected]/msg01970.html

Anthon
  • 78,313
  • 42
  • 165
  • 222
fsniper
  • 111
  • 1