简体   繁体   English

Windows使用MONO在LINUX中进行服务开发?

[英]Windows like services development in LINUX using MONO?

I just moved from .net development to LINUX MONO development... and i don have much experience with linux dev earlier.. 我刚从.net开发转移到LINUX MONO开发......我以前对linux开发人员有很多经验..

  1. I have a requirement to create a background service (like windows services) in mono c#.. is it possible.. 我要求在单声道c#中创建后台服务(如Windows服务)..是否可能..

  2. And is it possible to access the LINUX native APIs from mono c#. 是否可以从单声道c#访问LINUX本机API。 (like winAPI calls from win c#).. (比如来自win c#的winAPI调用)..

I use scripts, so I can capture the exit code and use it to perform automated updates and things. 我使用脚本,所以我可以捕获退出代码并使用它来执行自动更新和事情。 It also restarts itself if it crashes, and e-mails you when it restarts with the last x lines of the log file. 如果它崩溃,它也会自动重启,并在用日志文件的最后x行重新启动时给你发电子邮件。

/etc/init.d/MyMonoApp /etc/init.d/MyMonoApp

#!/bin/sh
#/etc/init.d/MyMonoApp
#

APP_NAME="MyMonoApp"
APP_PATH="/home/mono/MyMonoApp"

APP_USER=mono

case "$1" in
  start)


        echo "Starting $APP_NAME"

        start-stop-daemon --start \
                          --background \
                          --make-pidfile \
                          --pidfile /var/run/$APP_NAME.pid \
                          --chuid $APP_USER \
                          --exec "$APP_PATH/$APP_NAME"
    ;;
  stop)

        echo "Stopping $APP_NAME"
                start-stop-daemon -o  --stop \
                --pidfile /var/run/$APP_NAME.pid

    ;;
  *)
    echo "Usage: /etc/init.d/$APP_NAME {start|stop}"
    exit 1
    ;;
esac

exit 0

/home/mono/MyMonoApp /家庭/单声道/ MyMonoApp

#!/bin/sh
#!/home/mono/MyMonoApp

APP_NAME=`basename $0`
APP_DIR=`dirname $0`
HOSTNAME=`hostname`

cd $APP_DIR

tail --lines=300 output.log  | mail -s "MyMonoApp $HOSTNAME:$APP_NAME STARTED" "me@email.com"

exitcode=0
until [ $exitcode -eq 9 ]
do
        startdate="$(date +%s)"
        /usr/local/bin/mono MyMonoApp.exe $HOSTNAME:$APP_NAME > output.log
        exitcode=$?
        enddate="$(date +%s)"

        echo "EXIT CODE = $exitcode" >> output.log

        cp -f output.log output.log.1
        elapsed_seconds="$(expr $enddate - $startdate)"
        echo "Elapsed seconds $elapsed_seconds"


        subject="EXIT CODE: $exitcode"
        echo "BASH: Exit Code = $exitcode"

        if [ $exitcode -eq 6 ] #Restart
        then
          subject="RESTART"
        elif [ $exitcode -eq 7 ] #Previous version
        then
          subject="PREVIOUS VERSION"
          cp -fv MyMonoApp.exe_previous MyMonoApp.exe
        elif [ $exitcode -eq 8 ] #Update
        then
          subject="SOFTWARE UPDATE"
          cp -fv MyMonoApp.exe MyMonoApp.exe_previous
          mv -fv MyMonoApp.exe_new MyMonoApp.exe
        elif [ $exitcode -eq 9 ] #Shutdown
        then
          subject="SHUTDOWN"
        fi


        if [ $elapsed_seconds -ge 10 ]  #been running for longer than 10 seconds
        then
                tail --lines=300 output.log  | mail -s "MyMonoApp $HOSTNAME:$APP_NAME $subject" "me@email.com"
                sleep 1  # tiny delay to let things settle
        else
                sleep 5  # delay to protect against eating the CPU resourses
        fi


done

Note: if you close the app using the init.d script, it will kill the process, rather than signal it to cleanly close. 注意:如果您使用init.d脚本关闭应用程序,它将终止进程,而不是发出信号干净关闭。

  1. Mono ships with a Windows Service compatible system called mono-service. Mono附带一个名为mono-service的Windows Service兼容系统。

    • The Unix word for service is Daemon. 用于服务的Unix字是守护进程。 Regular daemons can be found in /etc/init.d/ and are installed into the runlevel they are supposed to run in by being symlinked from /etc/rc.* directories. 常规守护进程可以在/etc/init.d/中找到,并通过从/etc/rc.*目录中进行符号链接安装到它们应该运行的运行级别中。
  2. Just use p/invoke like you normally would. 只需像往常一样使用p / invoke。 You can also check out the source code of some other simple mono-based projects like Banshee to see how they do p/invokes on Linux. 您还可以查看其他一些基于单声道的简单项目(如Banshee)的源代码,了解它们在Linux上的调用方式。 Just search for banshee on google.com/codesearch. 只需在google.com/codesearch上搜索banshee即可。

For 1. - yes it is possible to create background service in mono c#. 对于1. - 是的,可以在单声道c#中创建后台服务。 Service is in fact a program that runs in background takes no input from keyboard and mouse, and does not output to directly to the user's screen. 服务实际上是在后台运行的程序不需要键盘和鼠标的输入,也不会直接输出到用户的屏幕。 After you create such program you can just run it with nohup ./programname & to set it to work into background and ignore the hangup signal (that is sent to your running processes when you log out). 创建此类程序后,您可以使用nohup ./programname运行它并将其设置为后台工作并忽略挂断信号(当您注销时发送到正在运行的进程)。

If you want to integrate it better, then you must write some additional scripts for stopping, starting, restarting it, etc (depending on your chosen linux distribution). 如果你想更好地集成它,那么你必须编写一些额外的脚本来停止,启动,重新启动它等(取决于你选择的linux发行版)。

As for LINUX (Unix api), you can use the Mono.UNIX library that is included with mono. 对于LINUX(Unix api),您可以使用mono附带的Mono.UNIX库 Although as a general rule you should try to stick with portable solutions instead of stuff like Mono.UNix or p/invoke whenever possible. 虽然作为一般规则,你应该尽可能坚持使用便携式解决方案而不是像Mono.UNix或​​p / invoke这样的东西。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM