An example of running a Bash Script on a Raspberry Pi using the jShell Library.
The Bash Script runs several commands which create output files. The content of these files are send to Home Automation solution.
#Bash Script rpiinfo.sh
#B4J Code Snippet
The Bash Script runs several commands which create output files. The content of these files are send to Home Automation solution.
#Bash Script rpiinfo.sh
B4X:
#/bin/bash
#Note:
#do not forget to x after change: sudo chmod a+x rpiinfo.sh. Then run ./rpiinfo.sh (to test prior using jShell Library)
#Get CPU Temperature - divide by 1000 in the B4J application
cat /sys/class/thermal/thermal_zone0/temp > rpicputemp.inf
#Get Disc space used
df -H | grep 'root' | awk '{ print $5 }' > rpidiscspaceused.inf
#Get RAM used
free -o -h | head -n2 | tail -n1 | awk '{print $3}' | tr -d [=M=] > rpimemused.inf
#Get RAM free
free -o -h | head -n2 | tail -n1 | awk '{print $4}' | tr -d [=M=] > rpimemfree.inf
#B4J Code Snippet
B4X:
'Get Raspberry Pi Information via Bash Script.
'The Bash Scripts creates several information files (text format, extension .inf)
Sub GetRasPiInfo
Try
shl.Initialize("shl", "./rpiinfo.sh", Null)
shl.WorkingDirectory = File.DirApp
shl.Run(-1)
Catch
Log(LastException.Message)
End Try
End Sub
Sub shl_ProcessCompleted (Success As Boolean, ExitCode As Int, StdOut As String, StdErr As String)
'Process successfully completed
'As an example start MQTT and update the RasPi information in Domoticz Home Automation after reading the Bash Script output files.
If Success And ExitCode = 0 Then
Log("Shell Command Successfull: MQTT connection initiated, data read and converted, published...")
Else
Log("Shell Command Error: " & StdErr)
End If
End Sub