Results 1 to 7 of 7

Thread: Daemonizing a Qt application

  1. #1
    Join Date
    Jun 2008
    Posts
    8
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Daemonizing a Qt application

    Greetings;

    I am trying to create a background application (starts with system startup, and keeps running in the background ) under linux. the only solution i found is to make it as daemon.
    searching the internet about how to create a daemon , I built a small app around my findings.

    Qt Code:
    1. #include <stdlib.h>
    2. #include <stdio.h>
    3. #include <fcntl.h>
    4. #include <signal.h>
    5. #include <unistd.h>
    6. #include "globaldefs.h"
    7.  
    8. void log_message(char *filename,char *message)
    9. {
    10. FILE *logfile;
    11. logfile=fopen(filename,"a");
    12. if(!logfile) return;
    13. fprintf(logfile,"%s\n",message);
    14. fclose(logfile);
    15. }
    16.  
    17.  
    18. /**
    19.   a signal handler for the Linux signals sent to daemon process,
    20.   for more signals, refer to http://www.comptechdoc.org/os/linux/programming/linux_pgsignals.html
    21.   */
    22. void signal_handler(int sig)
    23. {
    24. switch(sig) {
    25. case SIGHUP:
    26. log_message(LOG_FILE,"hangup signal catched");
    27. break;
    28. case SIGTERM:
    29. log_message(LOG_FILE,"terminate signal catched");
    30. break;
    31. }
    32. }
    33.  
    34. /**
    35.   create background process out of the application, source code taken from: http://www.enderunix.org/docs/eng/daemon.php
    36.   with some minor modifications
    37.   */
    38. void init_daemon()
    39. {
    40. int i,lfp;
    41. char str[10];
    42. if(getppid()==1)
    43. return; /* already a daemon */
    44. i=fork();
    45. if (i<0)
    46. exit(1); /* fork error */
    47. if (i>0)
    48. exit(0); /* parent exits */
    49.  
    50. /* child (daemon) continues */
    51. setsid(); /* obtain a new process group */
    52.  
    53. for (i=getdtablesize();i>=0;--i)
    54. close(i); /* close all descriptors */
    55. i=open("/dev/null",O_RDWR); dup(i); dup(i); /* handle standart I/O */
    56.  
    57. umask(027); /* set newly created file permissions */
    58.  
    59. chdir(RUNNING_DIR); /* change running directory */
    60. lfp=open(LOCK_FILE,O_RDWR|O_CREAT,0640);
    61. if (lfp<0)
    62. exit(1); /* can not open */
    63. if (lockf(lfp,F_TLOCK,0)<0)
    64. exit(0); /* can not lock */
    65. /* first instance continues */
    66. sprintf(str,"%d\n",getpid());
    67. write(lfp,str,strlen(str)); /* record pid to lockfile */
    68. signal(SIGCHLD,SIG_IGN); /* ignore child */
    69. signal(SIGTSTP,SIG_IGN); /* ignore tty signals */
    70. signal(SIGTTOU,SIG_IGN);
    71. signal(SIGTTIN,SIG_IGN);
    72. signal(SIGHUP,signal_handler); /* catch hangup signal */
    73. signal(SIGTERM,signal_handler); /* catch kill signal */
    74. }
    75. int main(int argc, char *argv[])
    76. {
    77. // first, create the daemon
    78. init_daemon();
    79. QCoreApplication a(argc, argv);
    80.  
    81. return a.exec();
    82. }
    To copy to clipboard, switch view to plain text mode 
    the code doesn't handle signals (for example, a termination signal). So, when I run it from the command line, and try to kill it, it refuses to get killed!!! the only way to kill the process is to restart my machine!!
    I really suspect that creating a QCoreApplication instance in the main method forks a new process other than the one i forked in the init_daemom() method.
    When I try to debug the application. it fails tp fork a new process thus exit() is issued and the debugging session terminates.

    I am using Qt 4.5 under ubuntu 9.04
    What am I missing? can there be anything done to make my Qt application run as a daemon process ?? Am i using a wrong way to do so?

    thanks in advance;

  2. #2
    Join Date
    Oct 2009
    Location
    Mexico
    Posts
    77
    Thanks
    6
    Thanked 10 Times in 10 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Daemonizing a Qt application

    try to use the follow Qt add-on
    http://qt.nokia.com/products/appdev/...ies/qtservice/

    use for create daemon in windows and linux,

    hope that find this useful

  3. #3
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Daemonizing a Qt application

    Just run your normal executable with &

    ./yourexe &

    or, if you don't process SIGHUP

    nohup ./yourexe &

    thats it. Nothing special needs to be done (So you don't need 'inidaemon' function)

    To kill, even if you trap signals like in your app above, just use kill:

    kill -9 <pid>

    You can't ignore or protect against kill -9

  4. The following user says thank you to squidge for this useful post:

    adutzu89 (14th March 2016)

  5. #4
    Join Date
    Dec 2009
    Posts
    19
    Thanks
    2
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Daemonizing a Qt application

    Quote Originally Posted by fatjuicymole View Post
    Just run your normal executable with &

    ./yourexe &
    You should also add a command line switch to your application to suppress console output in this case, since '&' just starts executable in background and still connects current tty device to its stdout and stderr. Thus if you run your application this way you will still see it's log messages output (unless you log out).
    Or you can manually redirect output with '>'.
    Last edited by nateriver; 1st January 2010 at 04:51.
    "Do be do be do", Frank Sinatra

  6. #5
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,677
    Thanks
    13
    Thanked 1,596 Times in 1,524 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Daemonizing a Qt application

    I'm not a guru UNIX programmer by any stretch but it seems that your setup routine connects the HUP and TERM signals (line 72-3) to a signal handler that does nothing with them except output a message and return. Consequently, the program does not exit for either signal.
    Last edited by ChrisW67; 1st January 2010 at 07:29. Reason: spelling error

  7. #6
    Join Date
    May 2010
    Posts
    12
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows Maemo/MeeGo

    Default Re: Daemonizing a Qt application

    How to add the QtService add-on ?

  8. #7
    Join Date
    May 2010
    Posts
    12
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows Maemo/MeeGo

    Default Re: Daemonizing a Qt application

    How to communicate from qt to this daemon in the above code?

Similar Threads

  1. Why some application needs server(-qws)
    By nrabara in forum Newbie
    Replies: 0
    Last Post: 17th December 2009, 14:01
  2. QT3 application is not running on ARM Board
    By soumya in forum Qt for Embedded and Mobile
    Replies: 16
    Last Post: 20th July 2009, 11:13
  3. Deploying Qt 4.5.1 Application on RHEL 5.. Pls Help
    By swamyonline in forum Installation and Deployment
    Replies: 0
    Last Post: 28th June 2009, 12:43
  4. dll + application
    By fpujol in forum Qt Programming
    Replies: 11
    Last Post: 15th April 2007, 19:37

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.