我有一些生产关键代码必须继续运行。
将代码视为
while (true){
init();
do_important_things(); //segfault here
clean();
}
我不能相信代码没有错误,我需要能够记录问题以便稍后调查。
这一次,我知道代码中的某个地方抛出了分段错误,我需要至少能够记录该错误,然后重新开始一切。
阅读这里有一些解决方案,但每个解决方案后面都是一场“激烈的战争”,声称该解决方案实际上弊大于利,没有真正的解释。我还找到了我考虑使用的this答案,但我不确定它是否适合我的用例。 那么,从 C++ 上的分段错误中恢复的最佳方法是什么?
Posix 示例:
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <cstdio>
#include <iostream>
int main(int argc, char* argv[]) {
if(argc < 2) {
std::cerr << "USAGE: " << argv[0] << " program_to_monitor <arguments...>\n";
return 1;
}
while(true) {
pid_t child = fork(); // create a child process
if(child == -1) {
std::perror("fork");
return 1;
}
if(child == 0) {
execvp(argv[1], argv + 1); // start the buggy program
perror(argv[1]); // starting failed
std::exit(0); // exit with 0 to not trigger a retry
}
// Wait for the buggy program to terminate and check the status
// to see if it should be restarted.
if(int wstatus; waitpid(child, &wstatus, 0) != -1) {
if(WIFEXITED(wstatus)) {
if(WEXITSTATUS(wstatus) == 0) return 0; // normal exit, terminate
std::cerr << argv[0] << ": " << argv[1] << " exited with "
<< WEXITSTATUS(wstatus) << '\n';
}
if(WIFSIGNALED(wstatus)) {
std::cerr << argv[0] << ": " << argv[1]
<< " terminated by signal " << WTERMSIG(wstatus);
if(WCOREDUMP(wstatus)) std::cout << " (core dumped)";
std::cout << '\n';
}
std::cout << argv[0] << ": Restarting " << argv[1] << '\n';
} else {
std::perror("wait");
break;
}
}
}
*/1 * * * * ! pgrep -x MyProg >/dev/null && /PathToMyProg/MyProg
Cron 将检查 MyProg 是否每分钟运行一次,如果停止则重新启动。