Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Windows like services development in LINUX using MONO?

Tags:

I just moved from .net development to LINUX MONO development... and i don have much experience with linux dev earlier..

  1. I have a requirement to create a background service (like windows services) in mono c#.. is it possible..

  2. And is it possible to access the LINUX native APIs from mono c#. (like winAPI calls from win c#)..

like image 820
RameshVel Avatar asked Aug 03 '09 07:08

RameshVel


People also ask

Does Mono work on Linux?

Installing from source Mono is supported on more platforms than there are binary packages. In particular, Mono works on Solaris, HP/UX, and PowerPC Linux. For these platforms, you must compile Mono from source. You can obtain source packages from the Mono web site's download page.

What is Mono used for in Linux?

Mono is a software platform designed to allow developers to easily create cross platform applications part of the . NET Foundation. Sponsored by Microsoft, Mono is an open source implementation of Microsoft's . NET Framework based on the ECMA standards for C# and the Common Language Runtime.

What is Monotechnology?

(2) (Mono) An open source cross platform implementation of Microsoft's . NET environment from Xamarin, which is a Microsoft company. Mono includes a just-in-time (JIT) runtime engine for Windows, macOS, Linux, iOS and Android and compilers for C# and Visual Basic.

What is Mono compiler?

Mono, the open source development platform based on the . NET Framework, allows developers to build cross-platform applications with improved developer productivity. Mono's . NET implementation is based on the ECMA standards for C# and the Common Language Infrastructure.


1 Answers

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.

/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

#!/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" "[email protected]"  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" "[email protected]"                 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.

like image 108
FlappySocks Avatar answered Oct 07 '22 00:10

FlappySocks