[Mono-dev] Writing a cross platform daemon in C#

mabra at manfbraun.de mabra at manfbraun.de
Sat Jan 9 15:23:12 EST 2010


Hi !

For the windows-part, write a service [which derives from ServiceBase]. To
make the source running in both worlds, mono must implement this and I think
[hope?], this is the case. To make a windows service running as a console
app - and so, it should run under linux - use this main method:

public static void Main(string[] args) //(Regard breaks, use full width)
{
	Service service = new Service();

	if(Environment.UserInteractive)
	{
		//Attach a control-c handler for the console to allow the
app to notify about exit.

		service.consoleControl = new ConsoleControl();
		ConsoleControl.ControlEventHandler controlEventHandler =
				new
ConsoleControl.ControlEventHandler(service.ConsoleControlHandler);
		service.consoleControl.ControlEvent += controlEventHandler;
		service.exitSignal = new ManualResetEvent(false);


		service.OnStart(args);	//For interactive use, call the
services "OnStart()" "manually" ...

		//Wait for the control-c handler for the console to allow
the app to notify about exit.

		service.WaitForExit();	//Do not do, how to make this under
(lin)(u)nix ;-)

		service.OnStop();
	}
	else
	{
		System.ServiceProcess.ServiceBase.Run(service);
	}
}

I do not know - under linux - how the end signal is finally received.
I use the obove to make minimal tests for services under windows.

Hope, this helps.

br--mabra


-----Original Message-----
From: mono-devel-list-bounces at lists.ximian.com
[mailto:mono-devel-list-bounces at lists.ximian.com] On Behalf Of Miguel de
Icaza
Sent: Friday, January 08, 2010 9:57 PM
To: paul at all-the-johnsons.co.uk
Cc: mono-devel
Subject: Re: [Mono-dev] Writing a cross platform daemon in C#

Hello,

> Does anyone have any examples of (or can point me to easy to understand
> examples of) daemons in C# and port monitoring in C#?

A daemon is merely a program that is not connected to a terminal or a
GUI.   All you need to do to launch a program as a daemon is to launch
it in the background either from a startup script on your system, or by
calling Mono like this:

	nohup mono program.exe &

Since daemons typically can not reliably log data to stdout/stderr, they
tend to write their data using syslog so the data ends up in a system
managed log, or even better, they dump the data in a well known
location, like this:

	nohup mono daemon.exe /home/logs/mylogs &

Or you can dump the data in a database (this might be a better idea).

Additionally, daemons ideally can be controlled remotely, the operations
on Linux are usually pretty trivial and are based on hooking up to
signals to receive very simple messages.   Usually the message means
"Relaod your configuration" or "Shut down propertly".

In this day and age, you might be better off just controlling your
daemon using a tiny protocol.   You could listen on a socket on a well
known port, and send command there, or if you are feeling Web two point
ohish, you can start an HttpListener and control your daemon by
navigating to http://localhost:YOURPORT/ and issuing commands there.

Miguel

_______________________________________________
Mono-devel-list mailing list
Mono-devel-list at lists.ximian.com
http://lists.ximian.com/mailman/listinfo/mono-devel-list




More information about the Mono-devel-list mailing list