我有两个Linux系统通过套接字(桌面和基于ARM的开发板)进行通信。
当服务器发送特定的预定义消息时,我希望重新启动(或重置)我的客户端应用程序(在开发板上运行)。我不想重启(重新启动) Linux,我只想让客户机应用程序自动重新启动自己。
我不明白该如何做。
发布于 2015-06-06 10:57:30
如果客户端应用程序是Linux服务,则可以使用以下命令重新启动:
service <clientapp> restart
或被迫重新加载其配置:
service <clientapp> reload
service <clientapp> force-reload
如果它更有可能是一个自定义应用程序,那么它需要在其代码中嵌入该特性,以便在接收到信号或事件时重新启动或重新加载其配置。如果不这样做,作为最后的手段,您总是可以杀死客户端应用程序:
pkill -9 <clientapp>
然后再重新启动,但它是丑陋的,因为它使应用程序处于一个未定的状态。
发布于 2020-04-16 07:57:17
如果您有程序的pid,那么可以使用"Kill PID“来终止应用程序。我试图在关闭之前保存应用程序的完整实例,但是这个过程非常繁琐。您可以简单地使用终端启动应用程序。就像你想启动火狐,只要输入终端就会打开你的应用程序。但是如果您希望从C++脚本重新启动。这可以通过简单地将命令传递到shell来完成。
我已经创建了一个函数,用于在shell中执行命令:
string ExecCmd(string command) {
char buffer[128];
string cmd_out = "";
// open pipe to file and execute command in linux terminal
FILE* pipe = popen(command.c_str(), "r");
if (!pipe)
return "popen failed!";
// read till end of process:
while (!feof(pipe)) {
// read output of the sent command and add to result
if (fgets(buffer, 128, pipe) != NULL)
cmd_out += buffer;
}
pclose(pipe);
// returns the output of terminal in string format
return cmd_out;
}
然后,对于终止这个过程并重新启动它,我使用了:
void RestartApplication(string proc_id) {
// kill the running process
ExecCmd("kill " + proc_id);
// restart the application by giving the name of your program
ExecCmd("nohup program_name >program_name.log &");
}
我使用nohup命令,因为它在命令执行后释放终端。现在,要调用这个脚本,可以在主程序线程下编写:
void main(){
RestartApplication(PID); // pass PID of your application to be restarted
}
https://unix.stackexchange.com/questions/207935
复制相似问题