在启动时作为 Linux 守护进程运行 Qt 应用程序

发布于 2024-07-26 18:34:10 字数 480 浏览 3 评论 0原文

我开发了一个 Qt 应用程序,其中包含 TCP 服务器等。 我现在正在尝试制作 Ubuntu 软件包并让应用程序在启动时自动启动。

即使没有人登录,应用程序也需要运行,这意味着守护进程通过 /etc/init.d/ 中的脚本启动

我尝试简单地在启动时运行应用程序并在 init.d 中停止时发送终止信号脚本,但这意味着应用程序在前台运行并阻止初始化脚本。

​​像其他问题一样分叉几乎似乎有效,我尝试启动 TCP 服务器后出现“未知错误”。 尽管如此,应该有一种简单的方法来编写一个初始化脚本,在各种 Linux 发行版上启动时在后台运行我的应用程序。

有人能指出我正确的方向吗?

将 Ubuntu 9.10 与 Qt 4.5 结合使用

I've developed a Qt application which contains a TCP server and such. I'm now trying to make Ubuntu packages and let the application automatically start on startup.

The application needs to be running even if nobody is logged in, which means a daemon started via a script in /etc/init.d/

I tried simply running the application on start and sending a kill-signal on stop in the init.d script but that means the application runs in the foreground and blocks the init-script.

Forking like in an other question almost seems to work, I get 'unknown error' after trying to start a TCP server. Nevertheless, there should be an easy to way to write a init-script that runs my application in the background on startup on the various Linux distributions.

Could anyone point me in the right direction?

Using Ubuntu 9.10 with Qt 4.5

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

叹倦 2024-08-02 18:34:10

最好的方法可能是使用 QtService< /a> 为您处理分叉工作。

但是,如果您想继续构建自己的应用程序,则应该将应用程序置于后台或通过 start-stop-daemon一起提供OpenRC 或适合您的发行版的类似实用程序。

另外,请确保您仅链接到 QtCore 共享库。 尽管应用程序可能是命令行并且永远不会启动 GUI,但这并不意味着应用程序的运行不需要 X。 例如,一组单元测试:

$ ldd runTests  | grep Qt
libQtTest.so.4 => /usr/lib/qt4/libQtTest.so.4 (0x00007fd424de9000)
libQtXml.so.4 => /usr/lib/qt4/libQtXml.so.4 (0x00007fd424baa000)
libQtGui.so.4 => /usr/lib/qt4/libQtGui.so.4 (0x00007fd4240db000)
libQtCore.so.4 => /usr/lib/qt4/libQtCore.so.4 (0x00007fd422644000)

因为存在 QtGui,所以也引入了所有 X 库,尽管从上面的输出中进行了过滤。

The best way is probably to use QtService where the work of forking is taken care of for you.

However, if you want to continue to build your own, you should either background the application or run it via start-stop-daemon that comes with OpenRC or a similar utility for your distribution.

Also, make sure that you only link to the QtCore shared library. Although the application might be command line and never pull up the GUI, that doesn't mean that X isn't required in order for the application to run. For example, a set of unit tests:

$ ldd runTests  | grep Qt
libQtTest.so.4 => /usr/lib/qt4/libQtTest.so.4 (0x00007fd424de9000)
libQtXml.so.4 => /usr/lib/qt4/libQtXml.so.4 (0x00007fd424baa000)
libQtGui.so.4 => /usr/lib/qt4/libQtGui.so.4 (0x00007fd4240db000)
libQtCore.so.4 => /usr/lib/qt4/libQtCore.so.4 (0x00007fd422644000)

Because QtGui is present, all the X libraries are also brought in, although filtered from the above output.

吃素的狼 2024-08-02 18:34:10

您的程序是 GUI 应用程序还是可以在没有 GUI 的情况下运行?

为什么不使用 & 在初始化脚本中将其设置为后台?

Is your program a GUI application or does it work without GUI?

Why don't you just background it within the init script using &?

忆梦 2024-08-02 18:34:10

您需要根据默认运行级别将符号链接添加到 /etc 下的任何 rc?.d 目录中。 或者使用 update-rc.d 脚本:首先您需要在 /etc/init.d 中创建一个执行应用程序的脚本; 其次,使用 update-rc.d 脚本添加启动所需的文件。

您可以通过阅读 update-rc.d 手册页找到有关如何执行此操作的信息:

$man update-rc.d

You need to add a symbolic link into any of the rc?.d directories under /etc depending on the default runlevel. Or use the update-rc.d script: first you need to create a script into /etc/init.d that executes the application; second, use the update-rc.d script to add the needed files to start.

You can find information about how to do it by reading update-rc.d manual page:

$man update-rc.d
随梦而飞# 2024-08-02 18:34:10

我认为最简单的方法是应用程序本身不具有任何守护进程逻辑,而是使用辅助程序在后台启动应用程序并为其管理 pid。

例如,startproc

I think the simplest way is to not have any daemonize logic in your application itself, instead use a helper program to start the app in the background and manage a pid for it.

For example, startproc.

琉璃繁缕 2024-08-02 18:34:10

您可以查看 /etc/init.d 中已有的许多脚本来获取灵感。 据我所知,大多数标准 Linux 守护进程都依赖 startproc 来启动,并依赖 killproc 来停止。

You can take a look at the many scripts already in your /etc/init.d for inspiration. From what I see there, most of standard linux daemons depend on startproc for start, and killproc for stopping.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文