Developing a Daemon for Mac with Qt and C++



  • Hi,

    I am trying to develop a dummy daemon for Mac OSX which should launch on startup and should support 'Stop,Resume, Pause'. For now, I have developed a code which is simply opening a text file and logging some data on it when the service starts. Unsure on how to stop it. Any advice on the best approach possible would be appreciated. Here is a little snippet.

    @
    void AppService::start()
    {
    try
    {
    QCoreApplication *app= application();
    qDebug()<<"Service has started!";
    qDebug()<< app->applicationDirPath();

        FILE *t;
        pid_t pid=0;
        pid_t sid=0;
        pid=fork();
        if (pid < 0) {
                exit(1);
        }
        if(pid>0)
        {
            std::cout<<"Service has started!";
            exit(0);
        }
    
        umask(0);
        sid=setsid();
        if(sid <0)
        {
            exit(1);
        }
    
        chdir("/");
        t=fopen&#40;"/Users/gecdelivery/projectfile/logfile.txt","w+"&#41;;
    
    
        close(STDIN_FILENO);
        close(STDOUT_FILENO);
        close(STDERR_FILENO);
    
        while(1){
        sleep(1);
        fprintf(t,"Service is running!\t Time %s\n",dt);
        fflush(t);
    }
    fclose(t);
    
    
    }
    

    @


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.