C# 如何编写 Mono 守护进程
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/186493/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I code a Mono Daemon
提问by Scott Cowan
I'm trying to write a Mono C# daemon for linux.
我正在尝试为 linux 编写一个 Mono C# 守护进程。
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.
编辑:我想出了如何在 debian 中使用 start-stop-daemon --background,所以我想我现在就使用它。
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.
编辑:我也在 java 中实现了这个,他们有这个很好的 addShutdownHook 可以捕获终止应用程序。我需要花更多的时间来整理 Mono 服务的依赖项,或者找到一种方法来捕获应用程序终止。
There is the SessionEnd event, but thats only available for services and not console apps
有 SessionEnd 事件,但这仅适用于服务而不适用于控制台应用程序
Answer:using mono-service to wrap a windows service on linux
采纳答案by lupus
You should implement a service and use mono-service. Google for it and you'll find several examples.
您应该实现一个服务并使用单一服务。谷歌一下,你会发现几个例子。
回答by 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 handlerusing UnixSignaland shutdown properly on receiving a certain signal. See the Mono FAQ, "Can I use signal handlers with Mono?"for caveats and an example.
一种更 unix-ish 的方法是使用UnixSignal注册信号处理程序并在接收到某个信号时正确关闭。请参阅Mono 常见问题解答,“我可以在Mono 中使用信号处理程序吗?” 对于警告和示例。
lupushas 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.
狼疮找到了mono-service,这是一个使用ServiceProcess
接口的托管进程。遗憾的是,这需要设置MONO_DISABLE_SHM
,这会禁用 Mono 中的某些功能,尤其是跨进程 IPC 系统。
回答by Dave Van den Eynde
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.
Linux 下的守护进程通常会侦听信号,例如 kill 信号,但还有其他守护进程允许它执行诸如软重启(读回配置)等操作。
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.
通常,这伴随着 /etc/init.d 目录中的脚本来控制启动和停止此类守护程序。通常在 /var/run 下创建一个 pid 文件,它保留脚本的进程 ID 以快速识别进程。
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.
即使在为 Mono 编码时,您也应该很好地了解您编码的环境,因为 Mono 进程或本机进程(例如,用 C 创建)或脚本之间没有区别。
Dave
戴夫
回答by godbyk
Miguel de Icaza recently wrote about a new Mono C# interactive shellthat you should be able to daemonize easily enough. Miguel has a follow-up articlewith 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.
Miguel de Icaza 最近写了一篇关于新的Mono C# 交互式 shell 的文章,您应该能够轻松地对其进行守护进程。Miguel 有一篇后续文章,其中包含一些源代码,展示了如何在其他 C# 应用程序中包含交互式 shell。它可以作为你的守护进程的一个很好的起点。
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.
请注意,交互式 shell 需要尚未发布的 Mono 2.2 版。然而,代码在Mono 的 svn 存储库中可用。
回答by Ana Betts
David is correct, stopping a service is accomplished via a UNIX signal, and you should use a signal handler to catch it.
David 是对的,停止服务是通过 UNIX 信号完成的,您应该使用信号处理程序来捕获它。
回答by miguel.de.icaza
To receive notifications in the Unix way, that is using signals, you want to use the Mono.Unix.UnixSignalfor each signal that you plan on receiving and then call UnixSignal.WaitAny () on an array of signals.
要以 Unix 方式接收通知,即使用信号,您希望对您计划接收的每个信号使用Mono.Unix.UnixSignal,然后在信号数组上调用 UnixSignal.WaitAny ()。
You would typically do this on a separate thread.
您通常会在单独的线程上执行此操作。
回答by 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.
作为替代方案,我使用 shell 脚本。它启动我的单声道应用程序,然后当我的应用程序退出(有意或无意)时,查看我的应用程序设置的任何返回信号。这可用于指示脚本在更新中复制、重新启动或退出。如果我的应用程序崩溃,则不会返回任何信号,因此脚本将重新启动我的应用程序并向我发送一封电子邮件,其中包含控制台输出的最后几行。