We have a C# mono program that is running on a raspberry pi 3. The program executes completely when I execute it manually, i.e, sudo mono /pathtoexe but when it executes on restart from rc.local (rc.local starts a script file, sudo /pathto .sh file, which in turn starts the application through sudo mono /paththexe) the program executes only partially with no visible error in the logs.
Point to note, the test case we are trying to overcome is making our application run without the internet since it works perfectly well if there is internet (Both on manual and auto-start)
The PI is a Raspberry PI 3 running Raspbian Version 8 (Jessie) Mono JIT compiler version 3.2.8 (Debian 3.2.8+dfsg-10)
Here is the rc.local file:
#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.
# Print the IP address
_IP=$(hostname -I) || true
if [ "$_IP" ]; then
printf "My IP address is %s\n" "$_IP"
fi
#echo ds1307 0x68 > /sys/class/i2c-adapter/i2c-1/new_device
#hwclock -s
#sudo /etc/init.d/watchdog start&
sudo /home/pi/Vbox/./Main.sh&
#sudo /home/pi/Vbox/./Cmu.sh&
#/usr/bin/mono-service -p:/home/pi/Vbox /home/pi/Vbox/VboxV2.exe
sudo iwconfig wlan0 txpower off
#sudo mono /home/pi/Vbox/Vbox.exe
#sudo python /home/pi/Audio/audio_play.py
#iptables-restore < /etc/iptables.ipv4.nat
exit 0
The main line here to note is sudo /home/pi/Vbox/./Main.sh&
The Main.sh file is:
#!/bin/bash
Date=`date +%d_%m_%Y`
ErrorLogPath="/home/pi/LOG/GatewayWrapperError_$Date.log"
LogPath="/home/pi/LOG/GatewayWrapperLog_$Date.log"
## Fail over Block
function Failover
{
ErrorIn=$1
Datetime=`date +%d-%m-%Y:%H-%M-%S`
echo "Error: $1 Program"
printf "$Datetime: Error executing $ErrorIn program. Sending E-Mail notification \n">>$ErrorLogPath
echo "Error: notification"
#sudo mono --runtime=v4.0 /home/pi/EXE/Email.exe &
#wait
printf "$Datetime: Gateway notification sent \n">>$ErrorLogPath
#sudo wvdial 3gconnect &
#sleep 30
echo "wvdial run succed"
#sudo reboot
}
function main
{
## Executing Net_Connection_Program Core
# {(
# ExecutionDateTime=`date +%d-%m-%Y:%H-%M-%S`
# printf "$ExecutionDateTime: Net_Connect.sh started. \n">>$LogPath
# echo "Net_Connect Started"
# sudo /home/pi/Wrapper/./NetConnect.sh &
# #sudo /home/pi/Wrapper/./test.sh &
# )} ||
# {(
# Failover "Net_Connect_Core"
#)}
echo "Started"
## Executing Gateway Core
{(
ExecutionDateTime=`date +%d-%m-%Y:%H-%M-%S`
printf "$ExecutionDateTime: ES900G started. \n">>$LogPath
echo "ES900G Started"
sudo mono --runtime=v4.0 /home/pi/Vbox/VboxV2.exe &> /home/pi/LOG/GatewayWrapperLog_24_09_2018.log
)} ||
{(
Failover "Gateway_Core"
)}
}
##Master Call
main;
When the program executes through the script, the logs stop at this point:
Function: Main Started
73574f
Vbox Version 1.0.0.0
ERROR: ConnectMQTT - Exception connecting to the broker
Here is the Main function in the code:
static void Main()
{
try
{
Thread.Sleep(5000);
Helper.WriteLine("Function: Main Started");
const string path = "/home/pi/Vbox/vboxid.txt";
VboxId = File.ReadAllText(path);
Console.WriteLine(VboxId);
Version = $"{Assembly.GetExecutingAssembly().GetName().Version}";
Helper.WriteLine($"Vbox Version {Version}");
CommMqttClient = null;
if (string.IsNullOrEmpty(VboxId)) return;
MQTT.ConnectMqtt(); //This is last error printed in the log shown above. The program seems to crash after this
Task.Factory.StartNew(VideoStorage.StartStorage);
Audio = new AudioPublicAnnouncement();
LiveStreaming = new VideoStreaming();
VideoRetrieve = new VideoRetrieve();
Console.ReadLine();
}
catch (Exception e)
{
Helper.WriteLine("Error - Main: " + e.Message, ConsoleColor.Red);
}
}
In the above code, if the program is executed Manually, it works. That is the main problem I'm facing..
Please note that this was written by the previous code maintainer and I was brought in to fix this problem. He is currently unavailable and I've been trying to solve this for the past 3 days. Any help would be hugely appreciated.
Thanks, Varun
EDIT: Latest code with infinite loop that doesn't halt execution:
static void Main()
{
try
{
Thread.Sleep(5000);
Helper.WriteLine("Function: Main Started");
const string path = "/home/pi/Vbox/vboxid.txt";
VboxId = File.ReadAllText(path);
Console.WriteLine(VboxId);
Version = $"{Assembly.GetExecutingAssembly().GetName().Version}";
Helper.WriteLine($"Vbox Version {Version}");
CommMqttClient = null;
if (string.IsNullOrEmpty(VboxId)) return;
MQTT.ConnectMqtt();
Task.Factory.StartNew(delegate
{
MQTT.PublishMessage("REBOOT", $"VBOXID:{VboxId};VERSION:{Version};TIME:{DateTime.Now:dd-MM-yyyy HH-mm-ss};", false);
});
Task.Factory.StartNew(VideoStorage.StartStorage);
Audio = new AudioPublicAnnouncement();
LiveStreaming = new VideoStreaming();
VideoRetrieve = new VideoRetrieve();
while (true)
{
Thread.Sleep(1000);
}
}
catch (Exception e)
{
Helper.WriteLine("Error - Main: " + e.Message, ConsoleColor.Red);
}
}
MQTT.ConnectMqttis supposed to connect to the internet somewhere? because for obvious reasons, that will never work if no networking is available at all. - X39Console.Write(...)debug outputs after the actual connect just to confirm - X39