Visit Our UNIX and Linux User Community


How to check if a unix text file is being accessed?


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers How to check if a unix text file is being accessed?
# 1  
Old 08-19-2002
How to check if a unix text file is being accessed?

I am writing a script that periodically reads in data from a text file. The only issue is that, that text file is periodically updated (appended to) by another script. I am using perl in UNIX environment. How can I check if that text file is being accessed, so I can wait until it is no longer being accessed before trying to access it. Know what I mean? Help woud be apprecated.

Ricky
# 2  
Old 08-19-2002
try fuser

The 'fuser' command shows if a file is in use and by who

hereby the SUN man pages of fuser

http://docs.sun.com/?q=fuser&p=/doc/...9vfiln4&a=view
# 3  
Old 08-19-2002
MySQL

use flock function on the file
if it returns a false, that means the file is being updated
so wait and try again later
if it returns a true, go ahead and carry out your operations

asif
# 4  
Old 08-19-2002
flock command in perl

I'm not sure if you use an error subroutine in case the file cannot be opened, closed, etc, but here's one possible way to code this:
Code:
open (file_label, "file_path") || Error('open', 'file');
flock (file_label, 1) || Error('lock', 'file');
my @array_name = <file_label>;
close (file_label) || Error ('close', 'file');

sub Error {
 print "Content-type: text/html\n\n";
 print "The server can't $_[0] the $_[1] : $! \n";
 exit;
}

This will request shared access to a file in order to read from it and then will store the whole file in an array.

Replace the '1' in the flock() command with a '2' if you want to request exclusive access to a file, in order to write to it.

added code tags for readability

Last edited by oombera; 02-19-2004 at 02:44 PM..
# 5  
Old 08-21-2002
Bug

Thanks to all,
Rickylui.
# 6  
Old 08-21-2002
lsof

LSOF is a good way to tell if the file is being accessed.


Here is a link I got from a google search.

http://ftp.cerias.purdue.edu/pub/too.../hpux/B.11.11/


From the man page:

Instead of a formatted display, lsof will produce output that can be
parsed by other programs. See the -F, option description, and the
OUTPUT FOR OTHER PROGRAMS section for more information.

In addition to producing a single output list, lsof will run in repeat
mode. In repeat mode it will produce output, delay, then repeat the
output operation until stopped with an interrupt or quit signal.



Hope this helps.


Smilie
# 7  
Old 08-21-2002
If you only read file, you should not worry about file locking and
accessing -- OS will take care of it. Anyway, when you write to the file from shell it is no reliable way from the OS point to determine, that all writes are done.
You can accomplish this programmatically: main shell create a flag file at the end of all writes, and subshell delete this flag file, as soon as it finished it's own processing.
 

Previous Thread | Next Thread
Test Your Knowledge in Computers #990
Difficulty: Medium
UNIX System V is one of the first commercial versions of the Unix operating system and was originally developed by AT&T and first released in 1981.
True or False?

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

File shows in ls but cannot be accessed

I can do an ls -l CD* and see the following CDTEST userA RW RW R and I an logged on as userA. But if I do an ls -l the file does not appear. If I do a vi CDTEST it tries to create a new file named CDTEST. If I do a mv or rm I get the message no file of name CDTEST found. Any idea what is going on.... (3 Replies)
Discussion started by: LeeT
3 Replies

2. UNIX for Advanced & Expert Users

How to find out last 5 users who have accessed a file?

Hi All, is there any command or script to find out last five users who have accessed a file thanks jcpratap (1 Reply)
Discussion started by: Jcpratap
1 Replies

3. UNIX for Advanced & Expert Users

Check EOF char in Unix. OR To check file has been received completely from a remote system

Advance Thanks. (1) I would like to know any unix/Linux command to check EOF char in a file. (2) Or Any way I can check a file has been reached completely at machine B from machine A. Note that machine A ftp/scp the file to machine B at unknown time. (5 Replies)
Discussion started by: alexalex1
5 Replies

4. Shell Programming and Scripting

check if file is being accessed by any application

Hello, is there maybe a way to check if a specific file is being accessed at the moment? Example: You will start copying a 10 GB file from A to B, how could I code it so my script actually knows when file B is finished copying? (1 Reply)
Discussion started by: TehOne
1 Replies

5. Shell Programming and Scripting

File Accessed Alarm ??

Hey, I want to ask a simple Question.... How would I be able to come to know that files/directoires in a Parent directory has been accessed (means contents of the file has been just viewed) by the user(s) in a group ? and mail the name(s) of those files/directories which has been accessed... (16 Replies)
Discussion started by: varungupta
16 Replies

6. AIX

File Accessed Alarm ??

Hey, I want to ask a simple Question.... How would I be able to come to know that files/directoires in a Parent directory has been accessed (means contents of the file has been just viewed) by the user(s) in a group ? and mail the name(s) of those files/directories which has been accessed... (1 Reply)
Discussion started by: varungupta
1 Replies

7. Shell Programming and Scripting

Can we get the list of users who accessed a file

Hi all, I will thankful if anybody helps me in finding out the users who accessed any of our files. Thanks in advance Chanakya (2 Replies)
Discussion started by: Chanakya.m
2 Replies

8. Shell Programming and Scripting

how to know which login file is being accessed

when unix is logged in it access some login file depending on the shell like .cshrc , .login , .vimrc , .bashrc how can we know which is being access in a perticular unix flavor. (1 Reply)
Discussion started by: useless79
1 Replies

9. Shell Programming and Scripting

how can i check if text file is closed ?

Hello all im trying to make daemon that listen for incoming textiles and then it will move them to defined directory now the daemon part is ok but im stack on how to check of file is close and done copied to directory . so i could process it and move it forward tnx (13 Replies)
Discussion started by: umen
13 Replies

10. UNIX for Dummies Questions & Answers

list file which is accessed yesterday

how can I use 'ls' to list file which is accessed yesterday? Thx :confused: (2 Replies)
Discussion started by: aaron_fong
2 Replies

Featured Tech Videos