Demonisation of a .NET Mono application on Linux

How to make a Linux-demon from a .NET Mono application.

tuxdemon

Sample solution is available for download here https://github.com/mchudinov/ServiceDemon.


Main program starts a service process that will stay active in memory and will periodically activate a job – do something.

servicedemon

Creating a service (demon) application on Linux using Mono is the same simple as creating it on Windows with pure .NET. Here is a basic MSDN article Walkthrough: Creating a Windows Service Application in the Component Designer about it. The same code will run on both Windows and Linux and the only difference is how to run the service itself.
To run a mono application as a service it must be started with a help of a special tool called mono-service.

Workflow:

In my sample application I use Quartz.NET scheduler to run tasks periodically.

Program class

Service extends ServiceBase class with scheduler

Debugging

Since I develop a service application debugging of it directly is complicated. I need a trick to simulate a service start. I can run OnStart() from a fake Main() method. This is described in an MSDN article How to: Debug the OnStart Method.

I do two modifications in the program:

  1. Surround main Main() method in the Program class with non-debug directives
  2. Add a second Main() method to the Service class and surround it with debug-only directives

Modified Program class

Second Main() method in the Service class

Now to debug the service switch to Debug configuration, set a breakpoint in the fake Main() and in the OnStart() methods.

Run the service

-l:LOCKFILE
Specifies the file to use for locking, the default is a filename constructed in /tmp based on the name of the program that hosts the service.

Stop the service

Just use kill UNIX command to terminate the process. Process id is stored in the lock file.

Start-stop script

Here is my simple start-stop script for a Mono service. Script must be included into C# solution and copied to the binary folder after build.
Script contains 3 procedures: usage, stop and start

Running script

serviceoutput

  • Oscar

    Hello.

    I’m trying to run a windows service on Debian 8, but I can’t. So I download your project but don’t work and i don’t know why. I compiled it with MonoDvelop 5.9.4 and Mono 4.0.2.

    Before compile I have updated Common.Logging, Common.Logging.Core and Quartz to 3.2.0 version.(MonoDevelop raises a warning if i don’t do it)

    After compile I execute:
    – root@PruebasMono:/etc/PruebasMono/ServiceDemon-master/ServiceDemon/bin/Debug# mono-service -l:/tmp/mymonoservice.lock ./ServiceDemon.exe

    But it don’t create the lock file.(I think if it don’t create this file it isn’t working)

    How can i execute your project??? Can you help me???

    Thanks and sorry for my English(i’m learning).

  • Roman

    Many thanks! Really good and helpful explanation. A small question though : why doesn’t your script remove the lock file after the process is stopped? Shouldn’t it be causing problems on attempt to start process again ?

  • Ivan

    I started service as explained. But when I reboot system (using Ubuntu 14.04) I noticed that process is stopped. Is mono-service capable to maintain itself? I mean, should use some other tool if I want to be sure that service will works properly after crash, rebooted or some other resaons?

  • You should use another tool to control the process restart.
    It might be Supervisor for instance.