How do I code a Mono Daemon

≯℡__Kan透↙ 提交于 2019-11-29 20:20:13

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

miguel.de.icaza

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.

David Schmitt

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.

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

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.

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

FlappySocks

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?

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!