The RunScript component allows you to run OS standard scripts.
If Gravio Server is running on Windows it can run batch files, Linux, Raspbian and macOS can run shell scripts.
If it is not Windows, you need to give execute rights to the uploaded file. The uploaded file is placed in <Gravio data folder>/actmgr/data/
, so
For the Gravio Folder in your operating system, consult the documentation about Path Handling
You can also pass variable values to the scripts, for example, to pass sensor values.
Parameters
Command | Required | The command you like to execute |
---|---|---|
Output | Required | The output of the command. cp.Output is set to either “stdout” or “combine”. |
Timeout | Required | Timeout |
OutputType | Required | Set the output type. cp.OutputType can be either “string” or “binary”. |
The output of the component is passed on to the cv.Payload
Macintosh Users
Macintosh users must give Gravio HubKit 4 full harddisk access in the security settings if they want to access files outside the Gravio folder. Please find the appropriate settings in your system preferences under the Privacy and Security settings:
Also, note that the Exec component invokes the sh
command, so you must ensure that your scripts and modules are loaded in that environment. For example, if you get an error similar to ModuleNotFoundError: No module named 'yourmodule' / Component: Exec: failed to run: exit status 1
you may like to ensure that either your sh
command works with your pyton script by adjusting path environment variables, or the module is installed system-wide using sudo.
If you have installed third party packages via npm
then you have to specify the file path of which you installed the package in. It will usually be in /usr/local/bin
so you will have to specify it in the exec
component. Here’s an example PATH=$PATH:/usr/local/bin package-name [option1] [option2]
. Please note that every package is used differently so you will need to understand how to use it first before executing the command lines from Gravio.
Example: Playing an MP3 File
Place an mp3
file in your /actmgr/data
folder and use the command afplay
(on mac) followed by the filename to play it:
Linux
Note, in Linux Gravio is running in a Docker container. This means that you may not be able to access the same environment as the shell script. This also means you will need to use different paths. You can see the relevant paths here. To recap: the folder /home/gravio/hubkitrepo4/data/actmgr/data/
on the host system is mounted on /var/opt/hubkit/actmgr/data/
on the guest system (Gravio).
If you run a Python script that relies on libraries, you may have to install those libraries first. In our example, we want to trigger a Python script that talks to the serial port. This requires the serial
package. In order to install that, you must run this shell script called install_pyserial_on_guest.sh
within the Gravio environment first:
#/bin/bash
# install pip3
if ! command -v pip3 &>/dev/null; then
apt update
apt install python3-pip
else
echo "pip3 found"
fi
# install pyserial
python3 -c "import serial"
if [ $? -ne 0 ]; then
pip3 install pyserial
else
echo "pyserial found"
fi
You need to run this within your Gravio Exec component prefixed by bash -c
, i.e. bash -c "/var/opt/hubkit/actmgr/data/install_pyserial_on_guest.sh"
That may take a little while, but after that, you can trigger your python scripts that rely on those libraries:
Using Named Pipes
You can also use Named Pipes (FIFOs) that are mounted on both, the docker as well as the host system, to pass trigger commands on the host system from the docker system. You can then use eval
on the host system to receive the commands coming from Gravio through the named pipe.
Step 1: Create a named pipe “file” (FIFO) by using the mkfifo
command somewhere in your mounted folder. For example, on the host system, enter:
sudo mkfifo /home/gravio/hubkitrepo4/data/actmgr/data/namedpipe
Step 2: Listen on the host system for the command to be executed. Move to the folder you just created the namedpipe
file and write:
while true; do eval "$(cat namedpipe)"; done
This will wait for anything that comes through the named pipe and execute it. Please bear in mind the necessary security restrictions for that.
Step 3: Simply echo
the command you want to execute on the host system to the Named Pipe from Gravio.
Of course, you would want the “listener” to be executed automatically every time the system boots. For this, you can create a script named execpipe.sh
somewhere on your system:
#!/bin/bash
while true; do eval "$(cat /home/gravio/hubkitrepo4/data/actmgr/data/namedpipe)"; done
and execute it every time the system boots by adding the following command to your crontab
by entering crontab -e
and adding this line:
@reboot /path/to/execpipe.sh
Important: be aware of any security considerations. More information about this solution can be found on https://stackoverflow.com/a/63719458/354204
Need more help with this?
Join our slack community for help