52

I'm trying to write a Mono C# daemon for linux.

I'd like to do a starts and stops of it when its done processing instead of just killing the process.

Does anyone have any examples of this?

Edit: I figured out how to use start-stop-daemon --background in debian, so I think I'll just use that for now.

Edit: I'm implementing this in java as well and they have this nice addShutdownHook that catches terminating the app. I need to spend a little more time sorting out the dependencies for mono service, or find a way to catch app termination.

There is the SessionEnd event, but thats only available for services and not console apps

Answer: using mono-service to wrap a windows service on linux

Community
  • 1
  • 1
Scott Cowan
  • 2,652
  • 7
  • 29
  • 45
  • I too would really love to see what the answer is to this one, I searched unsuccessfully a few months ago. – Joel Martinez Oct 09 '08 at 14:01
  • mono-service use old garbade collector whis is leaks in my 2.10 on debian. So I use cron in conjunction with mono-sgen. – ikutsin Jul 13 '11 at 13:45

7 Answers7

25

To receive notifications in the Unix way, that is using signals, you want to use the Mono.Unix.UnixSignal for each signal that you plan on receiving and then call UnixSignal.WaitAny () on an array of signals.


You would typically do this on a separate thread.

metadings
  • 3,798
  • 2
  • 28
  • 37
miguel.de.icaza
  • 32,654
  • 6
  • 58
  • 76
19

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

lupus
  • 3,963
  • 1
  • 18
  • 13
  • 18
    If the examples are easy to find, kindly link them here. A reference to Google is rather unstable over time. – fbmd May 24 '13 at 15:35
  • I found it easier to write a console application since it is then trivial to run the application in background in Linux - which makes it a "daemon". – markmnl Mar 27 '14 at 02:14
  • But how does this work if you've `mkbundle`'d your Mono application? – Cocowalla Apr 24 '14 at 14:31
  • 1
    Just for posterity. Here is how you can use POSIX (unix style service ) https://github.com/ServiceStack/ServiceStack/wiki/Run-ServiceStack-as-a-daemon-on-Linux And here is how you build a windows type service running on Unix http://stackoverflow.com/questions/15359190/c-sharp-service-as-a-daemon-in-debian-with-mono-service/15361225 – Archlight Jun 02 '14 at 07:47
  • I implemented Windows service application and I running on Ubuntu with mono-service. When I rebooted system my application stopped and I need to start mono-service again. Is it some common issue? Does somebody encountered with this? – kat1330 Mar 22 '16 at 21:09
16

A simple method would be to listen on a (local, high) port and receive commands from a management client, like bind does.

A more unix-ish way would be to register a signal handler using UnixSignal and shutdown properly on receiving a certain signal. See the Mono FAQ, "Can I use signal handlers with Mono?" for caveats and an example.

lupus has found mono-service, which is a hosting process using the ServiceProcess interfaces. Sadly this requires setting MONO_DISABLE_SHM, which disables some features in Mono, in particular cross-process IPC systems.

Community
  • 1
  • 1
David Schmitt
  • 58,259
  • 26
  • 121
  • 165
4

A daemon under Linux typically listens to signals, like the kill signal, but there are others that allow it to do things like a soft restart (reads back in configuration), and so forth.

Typically this is accompanied by a script in the /etc/init.d directory that controls starting and stopping such daemons. Typically a pid file is created under /var/run, that keeps the process id for the script to identify the process quickly.

Even when coding for Mono, you'd do well understanding the environment for which you're coding, since there's no difference between a Mono process or a native process (created in C, for example) or a script.

Dave

Dave Van den Eynde
  • 17,020
  • 7
  • 59
  • 90
0

Miguel de Icaza recently wrote about a new Mono C# interactive shell that you should be able to daemonize easily enough. Miguel has a follow-up article with some source code that shows how you can include the interactive shell in other C# applications. It may serve as a good starting point for your daemon.

Note that the interactive shell requires Mono version 2.2, which hasn't been released yet. The code is available in Mono's svn repository, however.

godbyk
  • 8,359
  • 1
  • 31
  • 26
0

David is correct, stopping a service is accomplished via a UNIX signal, and you should use a signal handler to catch it.

Ana Betts
  • 73,868
  • 16
  • 141
  • 209
0

As an alternative, I use a shell script. It starts my mono app, and then when my app quits (intentionally or unintentionally), looks at any return signals which my app has set. This can be used to instruct the script to copy in an update, restart or quit. If my app crashes, no signal is returned, so the script will restart my app and send me an e-mail with that last few lines of the console output.

See Windows like services development in LINUX using MONO?

Community
  • 1
  • 1
FlappySocks
  • 3,772
  • 3
  • 32
  • 33