How to Code a Mono Daemon

How do I code a Mono Daemon

You should implement a service and use mono-service. Google for it and you'll find several examples.

Windows like services development in LINUX using MONO?

  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.

C# service as a daemon in debian with mono-service

Try logging somewhere else than with System.Diagnostics.EventLog, perhaps to a text file or similar.

I can't find anything recent, but this post suggests that the EventLog is simply discarded when running on Linux; which would make sense.

EDIT:

Investigating the Mono source seems to bear this out. There are three possible options for event logging: win32, local and null. If the MONO_EVENTLOG_TYPE environment variable is set to local, then on Linux the logs should be written to /var/lib/mono/eventlog by default.

You really need to isolate your logging code from your expectations - it seems that your service works fine, but the logging is the problem.

.NET Service to Linux Daemon

You should create your programs from the beginning for use with Windows and Linux.

You need Visual Studio with Mono Tools or the free MonoDevelop-IDE to create a Mono-Application. MonoDevelop can import your Visual Studio Project. This IDE helps you to get the right namespaces.

Use Mono-Service to run your Assembly as daemon. Linux Daemons are using Signals to communicate with the System. Please read the documentation.

We already had a similar question, so please read this to see, how to process Unix-Signals.

using mono-service to wrap a windows service on linux

Where is your LD_LIBRARY_PATH pointing to? Is libMonoPosixHelper.so in there?

How to call Crypt() function mono C#?

Here's the complete working program which uses unix crypt:

using System;
using System.Runtime.InteropServices;

public class Test
{
[DllImport("libcrypt.so", EntryPoint = "crypt", ExactSpelling = true, CharSet = CharSet.Ansi)]
public static extern IntPtr UnixCrypt([MarshalAs(UnmanagedType.LPStr)]string key, [MarshalAs(UnmanagedType.LPStr)]string salt);

public static void Main()
{
var ptrResult = UnixCrypt("test", "test2");
Console.WriteLine(Marshal.PtrToStringAnsi(ptrResult));
}
}

It prints result of crypting with given key and salt. Of course you can put UnixCrypt in any other class. For convenience you can also create method:

public static string MyCrypt(string key, string salt)
{
return Marshal.PtrToStringAnsi(UnixCrypt(key, salt));
}

start-stop-daemon alway returns success

Your $? gets expanded in the context of the current shell together with $CMD and $FILE.

To fix this you'll have to escape it like \$?, i.e.

...  /bin/bash -c "$CMD; exec echo \$? >$FILE"

How to migrate a .NET Windows Service application to Linux using mono?

Under Linux, deamons are simple background processes. No special control methods (e.g start(), stop()) are used as in Windows. Build your service as a simple (console) application, and run it in the background. Use a tool like daemonize to run a program as a Unix daemon, and remember to specify mono as the program to be activated.

As noted by others, mono-service is a host to run services built with the ServiceProcess assembly. Services built for Windows can use this method to run unmodified under Linux. You can control the service by sending signals to the process (see man page).



Related Topics



Leave a reply



Submit