diff --git a/src/message_handler.cpp b/src/message_handler.cpp index 4adcc44..d493556 100644 --- a/src/message_handler.cpp +++ b/src/message_handler.cpp @@ -49,10 +49,22 @@ for (const auto &entry: fs::directory_iterator(fs::current_path())) { if (entry.is_regular_file() && entry.path().extension() == ".sh") { const auto script_path = entry.path(); - std::ostringstream cmd_ss; - cmd_ss << "./" + script_path.filename().string() << " > " << Config::SLAM_LOG_FILE << " 2>&1"; - const std::string command = cmd_ss.str(); - std::system(command.c_str()); + if (const pid_t pid = fork(); pid == 0) { + // 子进程执行脚本 + std::ostringstream cmd_ss; + cmd_ss << "./" + << script_path.filename().string() + << " > " + << Config::SLAM_LOG_FILE << " 2>&1"; + const std::string command = cmd_ss.str(); + + execl("/bin/sh", "sh", "-c", command.c_str(), nullptr); + // 如果执行失败 + std::cerr << "\033[33m" + << "Failed to execute script." + << "\033[0m" << std::endl; + exit(EXIT_FAILURE); + } break; } }