Operating System - HP-UX
1834331 Members
2215 Online
110066 Solutions
New Discussion

How to know if data is being accessed

 
SOLVED
Go to solution
Theresa Patrie
Regular Advisor

How to know if data is being accessed

Hi,
I have a script that runs periodically and changes permissions on new data that shows up in a certain directory. I find new data by comparing the output from a previous "ls" with the output from a new "ls". People will be continuously moving data into this directory. I was going to run my script periodically using cron, but how can I be sure that the new data I see using "ls" has completed its move and is not still in the process of being moved??
Thanks,
Theresa
P.S. I will be on Tuesday but look forward to reading your responses on Wednesday! Thanks!!
This is my easy job!
5 REPLIES 5
Sridhar Bhaskarla
Honored Contributor

Re: How to know if data is being accessed

Hi,

Oneway I would do is to use fuser command. If you use fuser against a file that is being accessed, it will print out the pid of the process. So, fuser returns anything, then you would skip copying that file or wait until it is done. You can accomplish it using a while statement with a sleep in it.

-Sri
You may be disappointed if you fail, but you are doomed if you don't try
Robert-Jan Goossens
Honored Contributor

Re: How to know if data is being accessed

Hi,

An other way is using losf.
take a look at the man page.

http://hpux.connect.org.uk/hppd/hpux/Sysadmin/lsof-4.64/

Hope it helps,

Robert-Jan.
A. Clay Stephenson
Acclaimed Contributor
Solution

Re: How to know if data is being accessed

You could use a find -newer against a reference file but here's my solution:

AGE=300
ls | while read FNAME
do
fileage.pl -m -s ${AGE} ${FNAME}
STAT=${?}
if [[ ${STAT} -eq 0 ]]
then
echo "${FNAME} has not been modified in ${AGE} seconds."
echo "It is safe to copy."
fi
done

Fileage.pl returns 0 if a file has not been modified in a given period; 1 if it has, and > 1 on error.


Invoke as fileage.pl -u for full usage.

If it ain't broke, I can fix that.
James R. Ferguson
Acclaimed Contributor

Re: How to know if data is being accessed

Hi:

A note of caution: Changing the name, the permissions or the ownership of a file or moving it from one directory to another updates the inode change timestamp as divulged by 'ls -lc'. Beware that 'fbackup' (if you use it) will also change this when it restores the lastaccess timestamp (ls -lu) of a file it copies.

Regards!

...JRF...
Theresa Patrie
Regular Advisor

Re: How to know if data is being accessed

Hi All,
Thanks for your responses.

I didn't try it, but I don't know if fuser would work if people were writing to this file/directory via nfs. I did, however, use the while statement with a sleep in it. Instead of fuser, I used Clay's suggestion of "find -newer my_touchfile" for a quick and dirty solution. It works great. The other suggestions may be better, but since I needed this done by this afternoon, I didn't have time to download lsof or to get the perl script working.
Thanks again!
Theresa
This is my easy job!