Daemon for Linux QT applications
Requirement: when we want to restart the application immediately after it is accidentally closed or aborted, we need the daemon to help.
P.s.: no virus, no virus ......
Implementation Scheme
1. Start the daemon first
2. Load and start the QT application in the daemon. In this example, the QT Application name is MyApp.
Special note
1. Start the QT application on the PC:
Execle ("./MyApp", "", null, Environ );
2. During my experiment on the arm board, the above statements cannot be executed normally and need to be further written. At the same time, the embedded Linux QT does not support X11 and qws is required:
Execle ("./MyApp", "./MyApp", "-qws", null, Environ );
Reference Code
#include <stdlib.h>#include <string.h>#include <unistd.h>extern char **environ;enum{ START = 0, WAIT, STOPPING,};int main(int argc, char **argv){ int status = 0, mode = START; pid_t pid ; while(1) { switch(mode) { case START: pid = fork(); if(!pid){ execle("./myApp","",NULL , environ); //execle("./myApp","./myApp","-qws",NULL,environ); } if(pid > 0) mode = WAIT; break; case WAIT: if(waitpid(pid, &status, 0) < 0) { mode = START; break; } else if(WIFSIGNALED(status)||WIFSTOPPED(status)||WIFEXITED(status)) { mode = START; }else{ mode = WAIT; } break; default: break; } } return 0;}