c++ - 终止程序但导致输出文件不被写入的 Shell 脚本

标签 c++ linux shell

我想在后台运行一个收集一些性能数据的程序,然后在前台运行一个应用程序。当前台应用程序完成时,它会检测到这一点并关闭后台应用程序。问题是当后台应用程序在没有先关闭文件的情况下关闭时,我假设文件的输出仍然是空的。有没有办法不断写入输出文件,以便在后台应用程序意外关闭时保留输出?

这是我的 shell 脚本:

./background_application -o=output.csv &
background_pid=$!

./foreground_application
ps -a | grep foreground_application
if pgrep foreground_application > /dev/null
then
     result=1
else
     result=0
fi

while [ result -ne 0 ]
do
     if pgrep RPx > /dev/null
     then
          result=1
     else
          result=0
     fi
     sleep 10
done
kill $background_pid
echo "Finished"

我可以访问用 C++ 编写的后台应用程序的源代码,它是一个基本循环并在每次循环迭代时运行 fflush(outputfile)。

最佳答案

这会更短:

./background_application -o=output.csv &
background_pid=$!
./foreground_application
cp output.csv output_last_look.csv
kill $background_pid
echo "Finished"

关于c++ - 终止程序但导致输出文件不被写入的 Shell 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36984739/

相关文章:

python - gammu 恢复期间检测到缓冲区溢出

linux - 如何使用 Inkscape 将带有汉字的 SVG 导出为 PNG?

linux - 如何清理以空格分隔的文本中未对齐的列?

C 将字符串映射到函数

c++ - 将指针数组作为参数,然后将其分配给一个字段

c++ - 如何在 C++ 中进行反向字符复制

linux - sed:如何删除与包含正斜杠的模式匹配的行?

bash - 使用 expect(1) 登录 SSH。如何退出 expect 并留在 SSH 中?

C++流作为成员变量

c++ - VC++ 中是否有 boost::shared_ptr 的本地/可靠替代方案?