繁体   English   中英

Shell脚本终止程序,但导致不写入输出文件

[英]Shell script to terminate program but causes output file to not be written

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

这是我的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"

暂无
暂无

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

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