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.
我开发了一个包含TCP服务器等的Qt应用程序。我现在正在尝试制作Ubuntu软件包,让应用程序在启动时自动启动。
The application needs to be running even if nobody is logged in, which means a daemon started via a script in /etc/init.d/
即使没有人登录,应用程序也需要运行,这意味着通过/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.
我尝试在启动时运行应用程序,并在init.d脚本中停止发送kill信号,但这意味着应用程序在前台运行并阻止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.
像在另一个问题中分叉几乎似乎工作,我尝试启动TCP服务器后得到'未知错误'。然而,应该有一种简单的方法来编写一个init-script,它在各种Linux发行版的启动时在后台运行我的应用程序。
Could anyone point me in the right direction?
有人能指出我正确的方向吗?
Using Ubuntu 9.10 with Qt 4.5
使用Utntu 9.10和Qt 4.5
5 个解决方案
#1
Is your program a GUI application or does it work without GUI?
您的程序是GUI应用程序还是没有GUI工作?
Why don't you just background it within the init script using &?
你为什么不在init脚本中使用&?
#2
The best way is probably to use QtService where the work of forking is taken care of for you.
最好的方法可能就是使用QtService来处理分叉工作。
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.
但是,如果您想继续构建自己的应用程序,则应该对应用程序进行后台处理,或者通过OpenRC附带的start-stop-daemon或类似的实用程序来运行它。
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:
此外,请确保您只链接到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)
Because QtGui is present, all the X libraries are also brought in, although filtered from the above output.
由于存在QtGui,所有X库也会被引入,尽管从上面的输出中过滤掉了。
#3
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.
您需要在/ etc下的任何rc?.d目录中添加符号链接,具体取决于默认的运行级别。或者使用update-rc.d脚本:首先需要在/etc/init.d中创建一个执行应用程序的脚本;第二,使用update-rc.d脚本添加所需的文件以启动。
You can find information about how to do it by reading update-rc.d manual page:
您可以通过阅读update-rc.d手册页找到有关如何操作的信息:
$man update-rc.d
#4
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.
我认为最简单的方法是在应用程序本身中不使用任何守护程序逻辑,而是使用帮助程序在后台启动应用程序并为其管理pid。
For example, startproc.
例如,startproc。
#5
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.
您可以查看/etc/init.d中已有的许多脚本以获取灵感。从我看到的,大多数标准Linux守护进程依赖于startproc开始,killproc用于停止。
#1
Is your program a GUI application or does it work without GUI?
您的程序是GUI应用程序还是没有GUI工作?
Why don't you just background it within the init script using &?
你为什么不在init脚本中使用&?
#2
The best way is probably to use QtService where the work of forking is taken care of for you.
最好的方法可能就是使用QtService来处理分叉工作。
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.
但是,如果您想继续构建自己的应用程序,则应该对应用程序进行后台处理,或者通过OpenRC附带的start-stop-daemon或类似的实用程序来运行它。
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:
此外,请确保您只链接到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)
Because QtGui is present, all the X libraries are also brought in, although filtered from the above output.
由于存在QtGui,所有X库也会被引入,尽管从上面的输出中过滤掉了。
#3
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.
您需要在/ etc下的任何rc?.d目录中添加符号链接,具体取决于默认的运行级别。或者使用update-rc.d脚本:首先需要在/etc/init.d中创建一个执行应用程序的脚本;第二,使用update-rc.d脚本添加所需的文件以启动。
You can find information about how to do it by reading update-rc.d manual page:
您可以通过阅读update-rc.d手册页找到有关如何操作的信息:
$man update-rc.d
#4
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.
我认为最简单的方法是在应用程序本身中不使用任何守护程序逻辑,而是使用帮助程序在后台启动应用程序并为其管理pid。
For example, startproc.
例如,startproc。
#5
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.
您可以查看/etc/init.d中已有的许多脚本以获取灵感。从我看到的,大多数标准Linux守护进程依赖于startproc开始,killproc用于停止。