To check if a file is open and in use (logs are being written to it)


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting To check if a file is open and in use (logs are being written to it)
# 1  
Old 10-13-2015
To check if a file is open and in use (logs are being written to it)

Hello Experts,

I need to write a shell script to check if a file is open and something is being written to it. I want to know how OS handles it. I checked with lsof command but it is not working. For a test I did this.

HTML Code:
while true; do echo `date` >>abc.txt; done
then I checked

lsof | grep -i /home/admin/abc.txt

It did not show any open process to me.

Yesterday when I tried the same thing, it once showed up i.e. I could see the output of lsof command but then again it disappeared.

I also tried the same by opening a file through vi but no luck.


I also want to know how system behaves for a file that is being open through vi, gedit etc and the one that is being appended through a loop or some script.
# 2  
Old 10-13-2015
How about fuser
# 3  
Old 10-13-2015
Quote:
Originally Posted by Peasant
How about fuser
Thanks for the advise. Can you please duplicate my testing script and show me how fuser handles this?
# 4  
Old 10-13-2015
Your loop constantly closes and opens the output file for append, so whether you see something with lsof becomes a race condition.

Instead try:
Code:
while true; do echo `date` ; done >abc.txt

or to append:
Code:
while true; do echo `date` ; done >>abc.txt

In both cases the output file is opened once and kept open the whole time for the duration of the loop.

Last edited by Scrutinizer; 10-13-2015 at 07:23 AM..
This User Gave Thanks to Scrutinizer For This Post:
# 5  
Old 10-13-2015
Quote:
Originally Posted by Scrutinizer
Your loop constantly closes and opens the output file for append, so whether you see something with lsof becomes a race condition.

Instead try:
Code:
while true; do echo `date` ; done >abc.txt

or to append:
Code:
while true; do echo `date` ; done >>abc.txt

In both cases the output file is opened once and kept open the whole time for the duration of the loop.
Thank you so much. That's what I was looking for. Now I will check in my script how I can I implement this.
# 6  
Old 10-13-2015
How about:
Code:
lsof abc.txt

Or, what you probably initialy (wanted to) tried:
Code:
lsof | grep -i abc.txt

Or even:
Code:
USERNAME=$USER
lsof /path/to/abc.txt | grep $USERNAME

To check if the file is open by a specific person.

hth
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to check if a file is open in editor?

Hi there! I'm developing a program that allows the user to open and edit files using both an editor and the terminal. Once the user has finished editing the file an update is sent to the logbook that compares the file before and after it was edited - this can only be done if the file is closed (I... (23 Replies)
Discussion started by: cherryTango
23 Replies

2. Shell Programming and Scripting

Need to check logs

I have nearly 25+ tail commands which we need to verify the logs if there is any errors on current or previous date with time. I need this to be automate and send email to me with details. Please help me on this. (5 Replies)
Discussion started by: Nasir HussainSM
5 Replies

3. Debian

Logrotate truncated my log files to 0 bytes and no logs are written

Hi, Yesterday I installed and configured logrotate on my Debian machine. I was expecting this to run at 06:25 in the morning and it actually did. All my old logs were compressed and zipped but the new logs were all with size equal to 0 bytes. Processes, while still running ok, they were not... (2 Replies)
Discussion started by: pmatsinopoulos
2 Replies

4. Solaris

Before I delete any file in Unix, How can I check no open file handle is pointing to that file?

I know how to check if any file has a unix process using a file by looking at 'lsof <fullpath/filename>' command. I think using lsof is very expensive. Also to make it accurate we need to inlcude fullpath of the file. Is there another command that can tell if a file has a truely active... (12 Replies)
Discussion started by: kchinnam
12 Replies

5. Solaris

logs to check

Hi all i want to know what are the logs we need to check when the server is down and how to resolve to make server UP? please help me with this (8 Replies)
Discussion started by: vkav
8 Replies

6. Shell Programming and Scripting

Lots of logs to move....don't remove if open

I have a ksh script that currently moves a day's worth of log files (about 15,000) files to a different directory. The issue is that about 100 of these files are still open for write when this happens. I need an efficient way to ensure that these files aren't open without doing an lsof on each... (7 Replies)
Discussion started by: nestafaria
7 Replies

7. Shell Programming and Scripting

How to check whether logs are updating or not?

how to check whether logs are updating or not in unix is there any built in command or function ? (1 Reply)
Discussion started by: mail2sant
1 Replies

8. Shell Programming and Scripting

avoid open file to check field.

Hi Everyone, # cat a.txt 94,aqqc,62345907, 5,aeec,77, # cat 1.pl #!/usr/bin/perl use strict; use warnings; use Date::Manip; open(my $FA, "/root/a.txt") or die "$!"; while(<$FA>) { chomp; my @tmp=split(/\,/, $_); if (index($tmp, "qq") ne -1) { ... (4 Replies)
Discussion started by: jimmy_y
4 Replies

9. UNIX for Dummies Questions & Answers

restrict data from getting written to Logs

$SYBASE/bin/isql -U $DB_USERID -S $DB_SERVER << ! >> $OUTFILE `echo $DB_PASSWD` use $db go Print " The processing" go ! # Extract data to file echo $DB_PASSWD | $SYBASE/bin/bcp $WRK_DB..open out $CONV_DIR/open".csv -t\, -c -U $DB_USERID -S $DB_SERVER -b 1000 | tail -3 I am able to... (0 Replies)
Discussion started by: w020637
0 Replies

10. Linux

check written data

I'm using growisofs to write DVD, e.g. $ growisofs -Z /dev/dvd -V "Personal Data, `date +"%b, %d %Y"`" -R -J /mnt/d/* How can I test whether data was written correctly? md5sum or so? (0 Replies)
Discussion started by: Hitori
0 Replies
Login or Register to Ask a Question