简体   繁体   English

C ++执行bash脚本,终止并重新启动当前进程

[英]C++ executing a bash script which terminates and restarts the current process

So here is the situation, we have a C++ datafeed client program which we run ~30 instances of with different parameters, and there are 3 scripts written to run/stop them: start.sh stop.sh and restart.sh (which runs stop.sh and then start.sh). 所以情况就是这样,我们有一个C ++ datafeed客户端程序,我们运行~30个具有不同参数的实例,并且编写了3个脚本来运行/停止它们:start.sh stop.sh和restart.sh(运行停止) .sh然后start.sh)。

When there is a high volume of data the client "falls behind" real time. 当存在大量数据时,客户端“落后”实时。 We test this by comparing the system time to the most recent data entry times listed. 我们通过将系统时间与列出的最新数据输入时间进行比较来测试。 If any of the clients falls behind more than 10 minutes or so, I want to call the restart script to start all the binaries fresh so our data is as close to real time as possible. 如果任何客户端落后超过10分钟左右,我想调用重启脚本来启动所有二进制文件,以便我们的数据尽可能接近实时。

Normally I call a script using System(script.sh), however the restart script looks up and kills the process using kill, BUT calling System() also makes the current program execution ignore SIGQUIT and SIGINT until system() returns. 通常我使用System(script.sh)调用脚本,但是重启脚本使用kill查找并终止进程,但是调用System()也会使当前程序执行忽略SIGQUIT和SIGINT,直到system()返回。

On top of this if there are two concurrent executions with the same arguments they will conflict and the program will hang (this stems from establishing database connections), so I can not start the new instance until the old one is killed and I can not kill the current one if it ignores SIGQUIT. 除此之外,如果有两个具有相同参数的并发执行,它们将发生冲突并且程序将挂起(这源于建立数据库连接),因此我无法启动新实例,直到旧实例被杀死并且我无法杀死如果忽略SIGQUIT那么当前的那个。

Is there any way around this? 有没有办法解决? The current state of the binary and missing some data does not matter at all if it has reached the threshold, I also can not just have the program restart itself, since if one of the instances falls behind, we want to restart all 30 of the instances (so gaps in the data are at uniform times). 二进制文件的当前状态和丢失的一些数据如果已经达到阈值则完全无关紧要,我也不能让程序自行重启,因为如果其中一个实例落后,我们要重启所有30个实例(因此数据中的间隙处于统一时间)。 Is there a clean way to call a script from within C++ which hands over control and allows the script to restart the program from scratch? 是否有一种干净的方法从C ++中调用脚本来移交控件并允许脚本从头开始重新启动程序?

FYI we are running on CentOS 6.3 仅供参考我们在CentOS 6.3上运行

Use exec() instead of system() . 使用exec()而不是system() It will replace your process with the new one. 它将用新的流程取代您的流程。 Note there is a significant different in how exec() is called and how it behaves: system() passes its string argument to the system shell to run. 请注意, exec()的调用方式和行为方式有很大不同: system()将其字符串参数传递给系统shell以运行。 exec() actually executes an executable file, and you need to supply the arguments to the process one at a time, instead of letting the shell parse them apart for you. exec()实际上执行一个可执行文件,你需要一次向进程提供一个参数,而不是让shell为你解析它们。

Here's my two cents. 这是我的两分钱。

Temporary solution: Use SIGKILL. 临时解决方案:使用SIGKILL。
Long-term solution: Optimize your code or the general logic of your service tree, using other system calls like exec or by rewritting it to use threads . 长期解决方案:使用其他系统调用(如exec)或通过重写它来使用线程来优化代码服务树的一般逻辑。

If you want better answers maybe you should post some code and or degeneralize the issue. 如果你想更好的答案,也许你应该张贴一些代码,或degeneralize问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM