27

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?

Iskustvo
  • 360
None
  • 589

4 Answers4

48

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
15

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.

Iskustvo
  • 360
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.

studog
  • 363
-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.

Roland Pihlakas
  • 159
  • 1
  • 1
  • 8