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.
#!/bin/bash
#Set container name manually or from stdin
#container=*replace with container name*
container=$1
lxcinfo="$(/usr/bin/lxc-info -n $container)"
if grep -q STOPPED <<< "$lxcinfo" ; then
echo 0
elif grep -q RUNNING <<< "$lxcinfo" ; then
echo 1
fi
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.