LXC EXEC to run NC in background not working

,

Trying to use LXC EXEC to start up a script (simple ncat webserver) that I want to run in the background. The script just listens on a certain port and pipes some text to the user connecting to the listening server.

what happens is that when I type the lxc exec – bash -c /root/script.sh & the prompt will not come back. I have tried lxc exec – bash -c ‘/root/script.sh &’ and a number of other variants. Nothing seems to work.

If I get onto the container I can run that script just fine.

What version of LXD are you using?

It works fine here on LXD 2.16:

stgraber@castiana:~$ time lxc exec keybase -- bash -c "/root/test.sh"
real	0m5.128s
user	0m0.013s
sys	0m0.015s

stgraber@castiana:~$ time lxc exec keybase -- bash -c "/root/test.sh &"
real	0m0.123s
user	0m0.013s
sys	0m0.012s

Note that it may be because your scripts uses stdin which would keep the connection active. In such case, try running it through “nohup” which should avoid that particular behavior (lxc exec test – bash -c “nohup /root/test.sh &”).

Stephane,

Thanks for the reply. I have tried many things and nohup was one of them. Here is me running the command you state.

root@ubuntu-LXD:~# time lxc exec centos7 – bash -c “/root/test.sh”

the prompt did not come back…killing the process…
real 0m22.662s
user 0m0.024s
sys 0m0.000s
root@ubuntu-LXD:~#
root@ubuntu-LXD:~# lxc --version
2.0.10

When I add the & it comes back but the script is not running in the back ground.

Tried it on a 2.16 system too
ubuntu@ip-10-99-1-37:~$ time sudo lxc exec test – bash -c “/root/test.sh”

again this is not working …

real 0m24.754s
user 0m0.020s
sys 0m0.016s
ubuntu@ip-10-99-1-37:~$ sudo lxc --version
2.16

Here is the script

#!/bin/bash

A sample Bash script, by jeff

while true
do echo -e “HTTP/1.1 200 OK\n\n $(date)” | nc -l -p 1500 >> /dev/null 2>&1
test $? -gt 128 && break
done

Not sure why you work and I don’t. This is a local 16.04 and a AWS instance of 16.04.

Jds

Ok, I think I managed to reproduce what you’re getting and a way to workaround it.

lxc exec test -- nohup bash -c "/root/test.sh &"

That will cause the bash shell to run detached from any of the exec session’s fds, it can then background fine and run the script.

3 Likes