Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run Qt application on startup as Linux daemon

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

like image 572
dutchmega Avatar asked Jul 05 '09 14:07

dutchmega


3 Answers

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.

like image 57
Kaleb Pederson Avatar answered Nov 15 '22 07:11

Kaleb Pederson


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

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

like image 35
ypnos Avatar answered Nov 15 '22 07:11

ypnos


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
like image 29
licorna Avatar answered Nov 15 '22 07:11

licorna