简体   繁体   English

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

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

I am wanting to run a program in the background that collects some performance data and then run an application in the foreground. 我想在后台运行一个程序,该程序收集一些性能数据,然后在前台运行一个应用程序。 When the foreground application finishes it detects this and the closes the application in the background. 前台应用程序完成时,它将检测到该情况,然后在后台关闭该应用程序。 The issue is that when the background application closes without first closing the file, I'm assuming, the output of the file remains empty. 问题是,假设后台应用程序在没有先关闭文件的情况下关闭时,文件的输出保持为空。 Is there a way to constantly write the output file so that if the background application unexpectedly closes the output is preserved? 有没有一种方法可以不断地写入输出文件,以便在后台应用程序意外关闭时保留输出?

Here is my shell script: 这是我的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"

I have access to the source code for the background application written in C++ it is a basic loop and runs fflush(outputfile) every loop iteration. 我可以访问用C ++编写的后台应用程序的源代码,它是一个基本循环,每次循环迭代都运行fflush(outputfile)。

This would be shorter: 这会更短:

./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