Run Qt application on startup as Linux daemon

dutchmega picture dutchmega · Jul 5, 2009 · Viewed 9.6k times · Source

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

Answer

Kaleb Pederson picture Kaleb Pederson · Aug 18, 2009

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.