用 C 停止和启动 Unix 守护进程
我正在尝试开发一个具有这些功能(命令行)的守护进程(Unix 平台):
user@unixbox>myDaemon start // This will start the daemon
user@unixbox>myDaemon stop // This will stop the daemon
user@unixbox>myDaemon show // This will show some stuff that the daemon is doing
如何实现这些功能?
I'm trying to develop a daemon (Unix platform) with these capabilities (command line):
user@unixbox>myDaemon start // This will start the daemon
user@unixbox>myDaemon stop // This will stop the daemon
user@unixbox>myDaemon show // This will show some stuff that the daemon is doing
How can these be implemented?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这通常是通过向守护进程传递信号来完成的。
您必须通过安装一个处理程序来选择要在守护进程中响应的特定信号(
SIGTERM
、SIGQUIT
、SIGSTOP
等)每次进程收到信号时调用。在 shell 中,您可以使用
kill(1)
命令向进程发送信号。请注意,如果您有活动的客户端/连接/作业,则优雅的守护进程停止可能会非常棘手。通常情况下,您应该停止接收新的邮件并等到最后一个邮件完成。
This is usually done by delivering signals to the daemon process.
You have to choose a particular signal to respond to in your daemon (
SIGTERM
,SIGQUIT
,SIGSTOP
, etc.) by installing a handler that is invoked each time the process receives the signal.From the shell you can send signals to a process using the
kill(1)
command.Note that a graceful daemon stop may be quite tricky if you have active clients/connections/jobs. Normally you should stop receiving new ones and wait until the last one is finished.