3

I am writing a long-running program in the Linux environment that will be calculating entries in a large table. Every time it comes to the end of the row, it outputs the calculated values into a plaintext file.

To avoid having to continually reopen the file and append to it, I am considering just opening the file once and holding it open for the duration of the program.

I know that there is a limit on the maximum number of file descriptors that can be open at once, but is there a time limit on a single file descriptor being held for an extended period of time?


Note: The process I am running could potentially take a month or more to complete.

2
  • If there was such a limit, your system would crash when it's reached as all processes have open files, starting with the first one (init) without which the system cannot run. Commented Nov 4, 2014 at 7:02
  • Excellent point: I'll accept this answer if you post it. Commented Nov 4, 2014 at 15:12

1 Answer 1

1

If I understand you correctly, you are looking for some solution as suggested over here. So, as suggested in this answer, I wanted to see if there is an option using exec. So, when I did a google search linux append using file descriptor, I got this answer from user Gilles.

So the essence of his answer is what I believe you are looking for.

exec 5>/tmp/foo       # open /tmp/foo for writing, on fd 5
while true; do        # 
  echo "Hello" >&5    # write to fd 5, i.e. /tmp/foo
done                  # 

Regarding the maximum time the file descriptor can remain open, I believe as long as the loop terminates it will remain open as we are not closing the file descriptor here. You could find more information on that answer that I had linked to.

5
  • To clarify, I was speaking more of a limit in the kernel, not how to do this programmatically from the shell. Commented Nov 4, 2014 at 15:14
  • @mattingly890, I know. That's what I have specified in my last paragraph. It will remain open as long as we are not closing the file descriptor. If you check the answer I linked, it discusses it in detail. Commented Nov 4, 2014 at 15:52
  • I appreciate your effort, but none of the questions or answers you linked to were clear and unequivocal about the behavior of the Linux kernel in my specific situation. I was not concerned about how to open a file indefinitely. I wanted a clear answer about whether the kernel had an internal time-to-live on open file descriptors. It is a case of mechanics vs behavior. Commented Nov 4, 2014 at 16:29
  • @mattingly890, agreed. I am just a curious learner trying to explore new things always. Sorry that this one did not answer your query :) Commented Nov 4, 2014 at 16:33
  • Has anyone found out about the time limit, theoretically or at least by trying? Commented Aug 4, 2019 at 8:59

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.