views:

699

answers:

4

Hi,

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#)..

Cheers

Ramesh Vel

+3  A: 

For 1. - yes it is possible to create background service in mono 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).

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).

Senad Uka
thanks Senad Uka,u mean, its going to be any normal windowless console app, and will be set to run in background using LINUX commands..
Ramesh Vel
+3  A: 
  1. Mono ships with a Windows Service compatible system called mono-service.

    • The Unix word for service is Daemon. 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.
  2. Just use p/invoke like you normally would. 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. Just search for banshee on google.com/codesearch.

wm_eddie
thanks eddie for your reply.. okie I ll try out with some samples..
Ramesh Vel
+1  A: 

As for LINUX (Unix api), you can use the Mono.UNIX library that is included with mono. Although as a general rule you should try to stick with portable solutions instead of stuff like Mono.UNix or p/invoke whenever possible.

Anders Rune Jensen
+1  A: 

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.

FlappySocks