Run the Qt application at startup as a Linux daemon

I developed a Qt application that contains a TCP server, etc. Now I am trying to make Ubuntu packages and automatically launch the application at startup.

The application must be launched, even if no one is registered, which means that the daemon was launched through a script in /etc/init.d/

I tried just starting the application at startup and sending a kill signal at a halt in the init.d script, but that means the application starts in the foreground and blocks the init script.

Viking, as in another question , seems to work, I get an “unknown error” after trying to start a TCP server. However, there should be an easy way to write an init script that runs my application in the background when it runs on various Linux distributions.

Can someone point me in the right direction?

Using Ubuntu 9.10 with Qt 4.5

+6
scripting qt daemon startup
source share
5 answers

Is your program a graphical application or runs without a GUI?

Why don't you talk about this in the init script with &?

+1
source share

The best way, probably, is to use QtService , in which care forking work takes care.

However, if you want to continue to create your own, you must either install the application or start it through start-stop-daemon , which comes with OpenRC or a similar utility for your distribution.

Also, make sure that you only reference the QtCore shared library. Although an application may be a command line and never pull up the GUI, this does not mean that X is not required to run the application. 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) 

Since QtGui is present, all of the X libraries are also included, although filtered out from the above output.

+2
source share

You need to add a symlink to any of the rc? .D directories in / etc, depending on the default runlevel. Or use the update-rc.d script: first you need to create a script in /etc/init.d that runs the application; secondly, use the update-rc.d script to add the necessary files to run.

You can find information on how to do this by reading the update-rc.d man page:

 $man update-rc.d 
+1
source share

I think the easiest way is to not have any daemonize logic in your application, instead use a helper program to run the application in the background and control the pid for it.

For example, startproc .

0
source share

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

0
source share

All Articles