Lxc: command not found when running a bash script

Does anyone know why this script being run on a debian 9 host with lxd 3.6 installed via snap…

#!/usr/bin/env bash
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/root

## install openssh-server on the container
lxc exec test-container -- apt install -y openssh-server python sudo

Returns this error?

line 5: lxc: command not found

With

$ which lxc
/snap/bin/lxc

you can see that the lxc client of the snap package is located in the /snap/bin directory.
Therefore, you need to add /snap/bin to the $PATH.
Normally, the shell’s $PATH has /snap/bin. In your script though, you set the $PATH to specific values, therefore you need to take care of adding /snap/bin in there yourself.

3 Likes

Perfect, thank you @simos

Sorry for recovering an old post but maybe it worths a mention.

Reverting from deb to snap lxc installation I was banging my head why server backup scripts stopped being able to be executed from home (remotelly). Could be ran locally though.
Its the same answer.
lxc exec db -- mysqldump --add-drop-table databasename | gzip > /home/username/mysqldump/databasename-$(date '+%d-%m-%Y').sql.gz
should become
/snap/bin/lxc exec db -- mysqldump --add-drop-table databasename | gzip > /home/username/mysqldump/databasename-$(date '+%d-%m-%Y').sql.gz

You can also adapt to

LXC=/snap/bin/lxc
GZIP=/bin/gzip

$LXC exec db -- mysqldump --add-drop-table databasename | $GZIP > /home/username/mysqldump/databasename-$(date '+%d-%m-%Y').sql.gz

The reason is that interactive users on a system with snaps, get /snap/bin/ into their $PATH through /etc/profile.d/apps-bin-path.sh.
If somehow this /etc/profile.d/apps-bin-path.sh does not run for you (like in cron), you do not automatically get /snap/bin into your $PATH.