You can reboot your server from the inside using fork. About the beauty of Unix.
Sort of:
int result = fork(); if(result == 0) DoServer(); if(result < 0) { perror(); exit(1); } for(;;) { int status = 0; waitpid(-1, &status, 0); if(!WIFEXITED(status)) { result = fork(); if(result == 0) DoServer(); if(result < 0) { puts("uh... crashed and cannot restart"); exit(1); } } else exit(0); }
EDIT:
It is probably wise to use the WIFEXITED macro as a test condition, which is more concise and portable (correspondingly modified code). Furthermore, it correctly models the semantics that we probably want.
waitpid , given zero flags, will return nothing but both normal and abnormal termination. WIFEXITED results in true if the process terminated normally, for example, returning from main or calling exit . If the process crashes (for example, because you requested it), it is perhaps very important that you do not continue to restart it until the end of days!
Damon source share