views:

2312

answers:

4

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

A: 

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

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

ypnos
+1  A: 

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
licorna
update-rc.d is distribution specific.
Kaleb Pederson
+1  A: 

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.

Kaleb Pederson
A: 

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.

Intransigent Parsnip