20

I am running a shell script that is writing to a file.
This script may take a long time to complete and I would like to monitor the partial output rather than wait for the whole script to finish.

Is it safe to open (double click) a file that is being written by the script?

  • 4
    That. Also if you know beforehand you could just pipe the scripts output though tee. – Hennes May 6 '18 at 21:58
  • 1
    Semi related is the tee commando: you can forward the output to some file and to the terminal at the same time: man7.org/linux/man-pages/man1/tee.1.html – Cheiron May 7 '18 at 14:19
42

Reading the file is safe, although double clicking you mentioned will probably open the file in some editor that will offer you an option to make changes and save them. Missclicks happen, so I recommend commands that won't even let you change the file.

These are examples that work in terminal; they will only read the file:

cat file
less file
less +F file
tail -n 5 file
tail -f file
  • 4
    A lot of smarter file editors will warn you that file has been changed and ask if you want to reload it, but dumb editors might not. – user1306322 May 6 '18 at 13:26
  • 5
    @user1306322 That's still not guaranteed to be safe. Juts because the file is open for writing doesn't mean its being written to constantly. If nothing was written while the editor has been running it won't notice. And since an editor may write a new file and rename on top (because renames are atomic), the file being written to will then have been deleted. Output written from that point onwards will go to a deleted file. – kasperd May 7 '18 at 9:11
13

As long as you are not writing to it, it should be okay.

However, I would recommend using

tail -f log_file

in another terminal.

This command will "follow" the file log_file and write the newly added content as soon as it is updated by the script.

1

Not enough rep to add a comment to Kamil Maciorowski's answer:

For some files you'll want tail -F file so that the following will continue through a rotation. Watching syslog for example.

-1

In case the script (or the underlying framework implementation) is repeatedly opening and closing the file handle during its work, it may encounter a sharing violation error in the subsequent write-mode open for the following reasons:
1) The script may request the write access in exclusive mode (excluding concurrent read access), so if your file editor/viewer program is even only reading the file yet, it may trigger a fault in the writing script.
2) Additionally, some file editors/viewers lock the file for the entire duration they have the file open.

In conclusion, there are possible problems that depend on the program you are using for viewing the file. And there are possible problems arising from the implementation (or the underlying framework) of the script that writes to the file.

  • 1
    From my experience this is very uncommon in Linux (note: I'm a home user). I know the issue does exist in Windows, the question is tagged linux though. What "editors/viewers" work like this in Linux? In what flavors of Linux are locks mandatory? – Kamil Maciorowski May 6 '18 at 18:22
  • @KamilMaciorowski Unfortunately, I have no specific warning list. Locks are not mandatory anywhere (neither are they under Windows), some people just use them. I am only saying that such problem is possible and please do not use this approach carelessly in mission critical situations. It is up to the OP to decide whether this applies or whether it is important. The mere existence of the above question raises the odds that such phenomenon has already happened for the OP. Answers claiming that nothing could happen, provide no much new information either. – Roland Pihlakas May 6 '18 at 18:28
  • 1
    @RolandPihlakas I'm willing to bet this answer's author has experienced it - but only on Windows. So it's irrelevant to POSIX-compliant systems like Linux, on which monitoring output via tail -f or less +F is routine. – Chromatix May 8 '18 at 9:07
  • @Chromatix I'm really confused about the constructive part of your reasoning and fail to see what kind of suggestion or new information are you providing here, besides your personal hate for Windows... You did not answer the points in my comment at all. Why do you care what I have experienced? Please do not get personal. – Roland Pihlakas May 8 '18 at 18:44
  • @RolandPihlakas Because your answer gives the very firm impression that opening a file read-only is dangerous on Linux, when it really isn't. In fact, it is always safe to read a file that's being appended to, as long as you don't assume that "end of file" means "end of record" or "end of line". The only case when it'll fail is when the writing application holds a "mandatory lock" which very few applications do (the standard POSIX lock is an "advisory lock") and which will result in a well-defined error being raised, rather than erratic behaviour. – Chromatix May 9 '18 at 1:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.