Hi,
I have attached a little sample service you can use as template for
your own tests.
It only writes a log file for all actions.
It can be compiled using:
mcs -out:TestService.exe -r:System.ServiceProcess.dll
-r:System.Configuration.Install.dll TestService.cs
If you don't need the Windows install functionality you can safely remove
the ProjectInstaller class and the reference to
System.Configuration.Install.dll.
Run it using;
mono-service TestService.exe
For more parameters see man mono-service
Controlling the service is easy:
Pausing: kill -USR1 `cat /tmp/TestService.exe.lock`
Continueing: kill -USR2 `cat /tmp/TestService.exe.lock`
Ending: kill `cat /tmp/TestService.exe.lock`
Have a look at the log to see any consequences of your actions because
there will be no feedback on the command line :-)
Errors go to syslog.
HTH,
Joerg.
2006/1/23, Oleg Deribas <[EMAIL PROTECTED]>:
> Hello,
>
> Mario Munda said the following on 23.01.2006 11:17:
>
> > I have to write an application running under FreeBSD, and I choose Mono,
> > because it is simple.. This app does not need GUI, and it has to run as
> > a deamon.
> > I know how to design and develop app, but i don't know how to set it to
> > run as a deamon.
>
> Use System.ServiceProcess namespace and mono-service to run it as daemon
> (man mono-service). I'm not sure if it have any differences on FreeBSD,
> but I've tested it on linux and it works as expected.
>
> --
> Oleg
>
>
> _______________________________________________
> Mono-list maillist - [email protected]
> http://lists.ximian.com/mailman/listinfo/mono-list
>
>
>
>
using System;
using System.Collections;
using System.ComponentModel;
using System.Diagnostics;
using System.ServiceProcess;
using System.IO;
using System.Configuration.Install;
using System.Threading;
namespace TestService
{
public class MainClass
{
private const string LogFile = "TestService.log";
// The main entry point for the process
static void Main()
{
Write2Log(null, "Main starting");
// Create an array of known services...
System.ServiceProcess.ServiceBase[] ServicesToRun;
ServicesToRun = new System.ServiceProcess.ServiceBase[] { new TestService("TestService") };
// ... and forward handling to ServiceBase.Run which triggers a
// callback to mono-service. This calls your OnStart and returns after all services
// were stopped.
System.ServiceProcess.ServiceBase.Run(ServicesToRun);
// This call is reached after all services were stopped
Write2Log(null, "Main ended");
}
public static void Write2Log(string category, string message)
{
using (StreamWriter w = new StreamWriter(LogFile, true, System.Text.Encoding.UTF8) )
{
if ( category != null )
w.WriteLine(category + " - " + message);
else
w.WriteLine(message);
}
}
}
// This is the implementation of the service to run.
// There can be more than one service inside of one EXE.
// mono-service is currently able to only run one of them
// simultaneously.
public class TestService : System.ServiceProcess.ServiceBase
{
private Thread mainThread = null;
private bool stopThread = false;
public TestService(string name)
{
this.ServiceName = name;
this.CanPauseAndContinue = true;
}
protected override void Dispose( bool disposing )
{
MainClass.Write2Log(ServiceName, "Dispose");
base.Dispose( disposing );
}
protected override void OnStart(string[] args)
{
MainClass.Write2Log(ServiceName, "Starting Service...");
mainThread = new Thread (new ThreadStart (_MainLoop));
stopThread = false;
mainThread.Start ();
}
protected override void OnStop()
{
MainClass.Write2Log(ServiceName, "Stopping Service...");
if (mainThread != null) {
stopThread = true;
mainThread.Join ();
mainThread = null;
}
}
protected override void OnPause()
{
MainClass.Write2Log(ServiceName, "Pausing Service...");
}
protected override void OnContinue()
{
MainClass.Write2Log(ServiceName, "Continuing Service...");
}
private void _MainLoop ()
{
while (!stopThread)
Thread.Sleep (500);
}
}
// This class allows easy installation of this service on Windows.
// It's currently not used on Linux/Mono.
[RunInstaller(true)]
public class ProjectInstaller : System.Configuration.Install.Installer
{
private System.ServiceProcess.ServiceProcessInstaller serviceProcessInstaller1;
private System.ServiceProcess.ServiceInstaller serviceInstaller1;
private System.ComponentModel.Container components = null;
public ProjectInstaller()
{
// This call is required by the Designer.
InitializeComponent();
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Component Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.serviceProcessInstaller1 = new System.ServiceProcess.ServiceProcessInstaller();
this.serviceInstaller1 = new System.ServiceProcess.ServiceInstaller();
//
// serviceProcessInstaller1
//
this.serviceProcessInstaller1.Password = null;
//
// serviceInstaller1
//
this.serviceInstaller1.DisplayName = "Test Service 1";
this.serviceInstaller1.ServiceName = "TestService1";
//
// ProjectInstaller
//
this.Installers.AddRange(new System.Configuration.Install.Installer[] {
this.serviceProcessInstaller1,
this.serviceInstaller1});
}
#endregion
}
}
_______________________________________________
Mono-list maillist - [email protected]
http://lists.ximian.com/mailman/listinfo/mono-list