I wanted a way to start/stop my LXC containers from pimatic. Here’s how I did it in case anyone else wants the same function. It should be noted I’m running pimatic on my home server which isn’t a raspberry pi.

      "id": "lxc-???",
      "name": "lxc-???",
      "class": "ShellSwitch",
      "onCommand": "lxc-start -n $container -d",
      "offCommand": "lxc-stop -n $container",
      "getStateCommand": "/path/to/lxcinfo $container",
      "interval": 10000

Replace $container with the name you chose for your container.

Then I made a script to parse the output of lxc-info and return a value 0/1 depending the state of the container.


#Set container name manually or from stdin

#container=*replace with container name*

lxcinfo="$(/usr/bin/lxc-info -n $container)"

if grep -q STOPPED <<< "$lxcinfo" ; then
echo 0

elif grep -q RUNNING <<< "$lxcinfo" ; then
echo 1


Don’t forget to make it executable.

chmod +x /path/to/lxcinfo

I run lxc as root because I need a privileged container. Also pimatic is run as root as well. So I created the folder /opt/pimatic/bin with lxcinfo inside.

I will try and make improvements. I hope someone else can use this.