Creating LXC containers via a shell script that is called from node.js

I have a shell script named that looks like this

echo "Creating container.."
lxc launch ubuntu:18.04 $1
lxc exec $1 -- sudo --user ubuntu --login

I deploy this script using Node.js via the exec function:
var containerName =;

const myShellScript = exec('sh ./ ' + containerName);
myShellScript.stdout.on('data', (data)=>{
myShellScript.stderr.on('data', (data)=>{

But, it simply runs the first echo command and not any of the other lxc commands.

Interestingly, directly running the shell file by doing sh ./ test, it seems to work.

What is causing this and how do I fix it?

Maybe take a look at the link below, I cant confirm but I expect its a similar problem.

You should think about using a “real” client library if your using node and not executing adhoc scripts :smile:

Beauty of LXD, or rather one of them, is that it has a REST API.
And luckily for you there are at least a couple of NodeJS API clients already available.
Example from the ts-lxd github page:
“The following example connects to the local LXC instance and launches a new container.”

import lxd from "ts-lxd";

const client = lxd();

(async () => {
  const container = await client.createContainer("myContainer", "ubuntu");
  await container.start();
  console.log("Started " +;

You probably need to close stdin as lxc launch will try and read the instance config from stdin.