在Linux中编写守护程序或服务的示例

时间:2022-07-31 23:55:43

I have been looking at daemons for Linux such as httpd and have also looked at some code that can be used as a skeleton. I have done a fair amount of research and now I want to practice writing it. However, I'm not sure of what can I use a daemon for. Any good examples/ideas that I can try to execute?

我一直在寻找像httpd这样的Linux守护进程,并且还查看了一些可以用作骨架的代码。我做了大量的研究,现在我想练习写作。但是,我不确定我可以使用守护进程。我可以尝试执行哪些好的例子/想法?

I was thinking of using a daemon along with libnotify on Ubuntu to have pop-up notifications of select tweets.

我正在考虑在Ubuntu上使用守护进程和libnotify来弹出选择推文的弹出通知。

  1. Is this a bad example for implementing a daemon?
  2. 这是实现守护进程的一个坏例子吗?

  3. Will you even need a daemon for this?
  4. 你甚至需要一个守护进程吗?

  5. Can this be implemented as a service rather than a daemon?
  6. 这可以作为服务而不是守护进程实现吗?

1 个解决方案

#1


First: PEP 3143 tries to enumerate all of the fiddly details you have to get right to write a daemon in Python. And it specifies a library that takes care of those details for you.

第一:PEP 3143试图列举你必须在Python中编写守护进程的所有细节。它指定了一个为您处理这些细节的库。

The PEP was deferred—at least in part because the community felt it was more a responsibility of POSIX or some Linux standards group or something to first define exactly what is essential to being a daemon, before Python could have its own position on how to implement one. But it's still a great guide. However, the reference implementation of that proposed library still lives on, as python-daemon, which you can install from PyPI.

PEP被推迟 - 至少部分是因为社区认为它更多是POSIX或某些Linux标准组的责任,或者首先确定什么是成为守护进程必不可少的东西,然后Python才能在如何实现它自己的位置一。但它仍然是一个很好的指南。但是,该提议的库的参考实现仍然存在于python-daemon中,您可以从PyPI安装它。


Meanwhile, the really interesting question for this project isn't so much service vs. daemon, as root vs. user. Do you want a single process that keeps track of all users' twitter accounts, and sends notifications to anyone who's logged in? Just a per-user process? Or maybe both, a single process watching all the tweets, then sending notifications via user processes?

同时,这个项目真正有趣的问题不是服务与守护进程,而是root与用户。您是否想要一个跟踪所有用户的Twitter帐户的流程,并向已登录的任何人发送通知?只是一个每用户进程?或者两者兼而有之,一个进程在观看所有推文,然后通过用户进程发送通知?

Of course you don't really need a daemon or service for this. For example, it could be a GUI app whose main window is a configuration dialog, which keeps running (maybe with a traybar thingy) even when you close the config dialog, and it would work just as well. The question isn't whether you need a daemon, but whether it's more appropriate. Which really is a design choice.

当然,你并不需要一个守护进程或服务。例如,它可能是一个GUI应用程序,其主窗口是一个配置对话框,即使在关闭配置对话框时也会保持运行(可能带有托盘栏),它也可以正常工作。问题不在于你是否需要一个守护进程,而是它是否更合适。这真的是一个设计选择。

#1


First: PEP 3143 tries to enumerate all of the fiddly details you have to get right to write a daemon in Python. And it specifies a library that takes care of those details for you.

第一:PEP 3143试图列举你必须在Python中编写守护进程的所有细节。它指定了一个为您处理这些细节的库。

The PEP was deferred—at least in part because the community felt it was more a responsibility of POSIX or some Linux standards group or something to first define exactly what is essential to being a daemon, before Python could have its own position on how to implement one. But it's still a great guide. However, the reference implementation of that proposed library still lives on, as python-daemon, which you can install from PyPI.

PEP被推迟 - 至少部分是因为社区认为它更多是POSIX或某些Linux标准组的责任,或者首先确定什么是成为守护进程必不可少的东西,然后Python才能在如何实现它自己的位置一。但它仍然是一个很好的指南。但是,该提议的库的参考实现仍然存在于python-daemon中,您可以从PyPI安装它。


Meanwhile, the really interesting question for this project isn't so much service vs. daemon, as root vs. user. Do you want a single process that keeps track of all users' twitter accounts, and sends notifications to anyone who's logged in? Just a per-user process? Or maybe both, a single process watching all the tweets, then sending notifications via user processes?

同时,这个项目真正有趣的问题不是服务与守护进程,而是root与用户。您是否想要一个跟踪所有用户的Twitter帐户的流程,并向已登录的任何人发送通知?只是一个每用户进程?或者两者兼而有之,一个进程在观看所有推文,然后通过用户进程发送通知?

Of course you don't really need a daemon or service for this. For example, it could be a GUI app whose main window is a configuration dialog, which keeps running (maybe with a traybar thingy) even when you close the config dialog, and it would work just as well. The question isn't whether you need a daemon, but whether it's more appropriate. Which really is a design choice.

当然,你并不需要一个守护进程或服务。例如,它可能是一个GUI应用程序,其主窗口是一个配置对话框,即使在关闭配置对话框时也会保持运行(可能带有托盘栏),它也可以正常工作。问题不在于你是否需要一个守护进程,而是它是否更合适。这真的是一个设计选择。