0

I have a program (c++) that outputs data to the terminal. I run it on a machine that I SSH to. I need to be able to:

  1. Run the program after I close my session
  2. Acess the data that has been output so far, while the program continues to output data.

Right now, I have been doing this:

nohup ./freqnew <testparams.txt &> testrun3.out&

It's almost what I want. It puts the data from the program into the file testrun3.out, and will run in the background after I close my terminal window. However, until the program is finished, the .out file is completely empty.

When I start doing long runs, they could take weeks, but I need to periodically access the data to make sure it's plausible.

Any ideas?

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190

3 Answers3

0

You should try flush() to push your data to disk: http://www.cplusplus.com/reference/ostream/ostream/flush/

Buddy
  • 10,874
  • 5
  • 41
  • 58
0

Since you're using the output the of program, you can use tee or script to redirect the output the a file and those apparently have better flushing capabilities (like in this answer).

That also lets you see the output on the screen as well as saving it to disk (if that's a desirable feature).

nohup ./freqnew <testparams.txt 2>&1 | testrun3.out
Community
  • 1
  • 1
Buddy
  • 10,874
  • 5
  • 41
  • 58
0

One option is to redirect cout to the file that you want:

std::ofstream out("out.txt");
std::streambuf *coutbuf = std::cout.rdbuf(); //save old buf
std::cout.rdbuf(out.rdbuf()); //redirect std::cout to out.txt!
Fantastic Mr Fox
  • 32,495
  • 27
  • 95
  • 175