自动执行时,C#Mono程序在树莓派上崩溃

时间:2018-09-24 07:58:47

标签: c# linux raspberry-pi mono

我们有一个在树莓派3上运行的C#mono程序。当我手动执行该程序时,该程序会完全执行,即sudo mono / pathtoexe,但是在从rc.local重新启动后执行(rc.local启动一个脚本文件sudo / pathto .sh文件,该文件又通过sudo mono / paththexe启动应用程序),程序仅部分执行,日志中没有可见错误。

需要注意的是,我们要克服的测试用例是使应用程序在没有互联网的情况下运行,因为如果有互联网(手动和自动启动),它都能很好地运行

PI是运行Raspbian版本8(Jessie)的Raspberry PI 3。 Mono JIT编译器版本3.2.8(Debian 3.2.8 + dfsg-10)

这是rc.local文件:

#!/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

要注意的主线是 须藤/ home / pi / Vbox /./ Main.sh&

Main.sh文件为:

#!/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;

当程序通过脚本执行时,日志将在此时停止:

Function: Main Started
73574f
Vbox Version 1.0.0.0
ERROR: ConnectMQTT - Exception connecting to the broker

这是代码中的Main函数:

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);
            }
        }

在上面的代码中,如果程序是手动执行的,则它可以工作。那是我面临的主要问题。

请注意,这是由以前的代码维护者编写的,我是来解决此问题的。他目前不可用,过去3天我一直在尝试解决此问题。任何帮助将不胜感激。

谢谢, 瓦伦

编辑:具有无限循环且不会停止执行的最新代码:

 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 个答案:

答案 0 :(得分:0)

我将此问题标记为已解决。不幸的是,这与代码的执行方式有关。详细信息在问题的最后编辑中。 @knocte是的,尽管这是一个跨500多个站点运行的生产系统,但我们需要尽早进行此操作,因此我们需要采取有计划和有计划的方法。 感谢您的所有投入。非常感激。干杯!