0
votes

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);
            }
        }
1
So ... what is the problem here? I guess MQTT.ConnectMqtt is supposed to connect to the internet somewhere? because for obvious reasons, that will never work if no networking is available at all. - X39
The problem here is that if I execute the mono application, sudo mono VboxV2.exe through the console, it works! It throws an appropriate exception at that point and continues executing. The program has some tasks to execute even if there is no internet. It's only when the program is run on startup that it stops midway. - Varun Keshav
I highly doubt that it actually just "stops" for no reason unless it gets aborted. Have you checked if it may just continue but due to some OS reasons it just does not correctly does what it is supposed to do? Maybe add some Console.Write(...) debug outputs after the actual connect just to confirm - X39
Did you ever checked execution and group permissions for that file? Permisions granted could be different when executed from mono or standalone. - Carles
Thank you for your comments. But it seems like the problem was in the code itself. i.e, the classes. Since it was a static method being called, seems like if I add an infinite loop with some delay, all methods are executed, then the program continues to execute. Posting the latest code that makes execution work. Although I still don't know why. - Varun Keshav

1 Answers

0
votes

I'm marking this issue as solved. Unfortunately, it has to do with the way the code was being executed. Details are in the last edit of the question. @knocte Yes, we need to do this asap although since it is a production system running across 500+ sites, we need to take a calculated and planned approach to it. Thank you for all your inputs. Much appreciated. Cheers!