Operating System - HP-UX
1825643 Members
3647 Online
109685 Solutions
New Discussion

Re: How can I know that a file is being accessed ?

 
SOLVED
Go to solution
Brecht De Baets
Frequent Advisor

How can I know that a file is being accessed ?

Hello,

we have a HP-UX system B.11.23.

I would like to know if I can issue a statement to know if a pdf-file is being accessed by Adobe Reader.

The files are placed on a windows file-server which is mounted in our unix. Sometimes files are accessed by users (mostly using Adobe Reader).

In Windows you cannot delete a file which is being accessed. In unix the file is always deleted, even if it is being accessed. That can be a problem for us.

Is there a way in unix to know whether the file is being accessed ?
17 REPLIES 17
Dave Hutton
Honored Contributor

Re: How can I know that a file is being accessed ?

Could do it two ways. lsof or fuser:
servera:root /opt/ignite/share/doc # fuser diskmirror.pdf
diskmirror.pdf: 3440o

servera:root /opt/ignite/share/doc # ps -ef | grep 3440
root 4202 3472 1 11:08:16 pts/2 0:00 grep 3440
root 3440 3389 0 11:05:05 pts/0 0:03 /opt/Acrobat5/Reader/hppahpux/bin/acroread


Or you could do the same type of thing with lsof | grep diskmirror.pdf

servera:root /opt/ignite/share/doc # lsof | grep -i diskmirror.pdf
acroread 3440 root 24r VREG 64,0x5 67407 17271 /opt/ignite/share/doc/diskmirror.pdf

OldSchool
Honored Contributor

Re: How can I know that a file is being accessed ?

I'm not sure I understand. Given that the file is shared between unix and windows, are you trying to determine (from the unix side) if a someone currently has the file opened in windows?
Shannon Petry
Honored Contributor

Re: How can I know that a file is being accessed ?

This is a somewhat complex issue, but I will try and explain, but it is not really a Unix problem.

In Windoze, a reader accessing the file marks it open. I assume that you are using CFS or something similar to share this Unix file system to multiple users. The Windows file system reports the file being open to the Windows system, but not to Unix.

The Unix side will delete the file do to a lack of a file lock.

You could make an enhancement to Microsoft for proper NFS file system support at version 2 or higher.. But good luck with that one.
Microsoft. When do you want a virus today?
Shannon Petry
Honored Contributor

Re: How can I know that a file is being accessed ?

This is a somewhat complex issue, but I will try and explain, but it is not really a Unix problem.

In Windoze, a reader accessing the file marks it open. I assume that you are using CFS or something similar to share this Unix file system to multiple users. The Windows file system reports the file being open to the Windows system, but not to Unix.

The Unix side will delete the file do to a lack of a file lock.

You could make an enhancement request to Microsoft for proper NFS file system support at version 2 or higher.. But good luck with that one.
Microsoft. When do you want a virus today?
Dave Hutton
Honored Contributor

Re: How can I know that a file is being accessed ?

I must of been a little distracted, but I didn't see Adobe was on windows from a fileshare. I would still assume there would be something from the unix side suggesting that the file was being in use. Instead of my example of the motif version of Adobe localling on the server I had the pdf file, I would think you would see maybe the owner of the file nfs/samba. Unfortunatly I can't mimic what your environment has going.
OldSchool
Honored Contributor

Re: How can I know that a file is being accessed ?

Dave..I'm not sure it is, which is why I asked for clarification from the poster. still waiting....
Brecht De Baets
Frequent Advisor

Re: How can I know that a file is being accessed ?

Hello,

Thanks for the replies so far. I will try to clarify my issue.

We are working with an oracle application server and an oracle database which runs on a Unix server.

We have a windows-file server with pdf-files. We have written an oracle web-application where users can view the pdf-files. I want the pdf-file to be deleted after it is opened (by adobe reader).
The only way I can do this is, is by giving a Unix-command (rm) because I need to write PL/SQL that is turning on my database which is on a unix server.

The problem I am having is that when I issue the rm-command the file is not yet opened on the client, resulting in a 'no file found' (the file is deleted too early). So I need something to test if the file is being accessed, so my application can wait until the the file is accessed and then delete the file afterwards.

Best Regards,
Brecht De Baets
Frequent Advisor

Re: How can I know that a file is being accessed ?

I think it must be possible. With ll -u you can see the time that the file was last accessed.
But I can't use that because I cannot test on the time. I need a status or something that tells me that the file is being accessed.

Is there some statement that gives me a status ?

Regards,
Brecht
OldSchool
Honored Contributor

Re: How can I know that a file is being accessed ?

I don't know of anything on the ux side that will tell you the file is being accessed on hte windows side of the share.

So if you could state what problem you are trying to address, maybe folks here can offer some alternatives.
OldSchool
Honored Contributor

Re: How can I know that a file is being accessed ?

note that it may depend on how the files are shared.. are they on samba shares or nfs or ????
Brecht De Baets
Frequent Advisor

Re: How can I know that a file is being accessed ?

It is an NFS-share.

I have described my probem in detail in one of my previous posts (20 december). I do not know what more I can say ?

But the statement ll -u gives me almost what I need. It just gives me the time, but I cannot test on the time to see if the file is currently being accessed (as I have to wait until the file is being accessed to delete it).

Regards
OldSchool
Honored Contributor

Re: How can I know that a file is being accessed ?

ok, the difference between the output between "ls -l" and "ls -u" *might* be of some use.

one other strategy that might work. Create your pdf, then create another file of the same name (but w/o the .pdf extension) like myfile.pdf and myfile. The of creation must be pdf first, then the other file.

then something like

find / -name -newer a a

*might* work as well. I didn't check the syntax or results, so consult the man pages as necessary.

regarding your dec 20 post....yes, I read it. It seems we have our wires slightly crossed.

The statement " cannot test on the time to see if the file is currently being accessed (as I have to wait until the file is being accessed to delete it)." is closer to what I'm asking, so lets try again...

I'm looking for a description like: I have a requirement < something >. To accomplish this, I have chosen to delete the file as soon as possible. Files may be deleted once the following criteria are satisfied:"

What I'm looking for is "" in the above statement, as well as delete criteria. What I'm getting at is that there may exist other methods to accomplish what you want, but doing it in another manner.

The method you've been talking about (waiting to delete until the file is accessed) has lots of potential pitfalls...for example: what does your process do if the file isn't opened in a timely fashion (i.e. user walks away, closes window or whatever)? Does it hang there waiting???
Hein van den Heuvel
Honored Contributor

Re: How can I know that a file is being accessed ?

>> Is there a way in unix to know whether the file is being accessed ?

Sounds to me that all you need to know is whether it has been accessed.

Perhaps you can 'stat' the file and look for st_atime /* Time of last access */ being more recent than st_mtime /* Last modification time */.

IN perl you would use the "-X" file functions for those.

Unfinished perl code:

foreach (<*.rpt>) {
unlink if (-M > 1) || (-A < -M);
}

That is pick up a file to test in $_
If older then a day (1) delete.
If access less that modify then delete.

Below the signature some example values to explain. Note, the small (recent) time values are express with a float and 10 ** -5, as 1 second is 1/86400 :
$ perl -le 'print 1/86400'
1.15740740740741e-05

hth,
Hein.


$ cat > /tmp/x
aap
noot
mies
$ perl -e '$_ = shift; print -M, "\n", -A, "\n", -C , "\n"' /tmp/x
0.000162037037037037
0.000219907407407407
0.000162037037037037
$ perl -e '$_ = shift; print -M, "\n", -A, "\n", -C , "\n"' /tmp/x
0.000243055555555556
0.000300925925925926
0.000243055555555556
$ cat /tmp/x
aap
noot
mies
$ perl -e '$_ = shift; print -M, "\n", -A, "\n", -C , "\n"' /tmp/x
0.000509259259259259
8.10185185185185e-05
0.000509259259259259
$ chmod +r /tmp/x
$ perl -e '$_ = shift; print -M, "\n", -A, "\n", -C , "\n"' /tmp/x
0.000798611111111111
0.00037037037037037
4.62962962962963e-05
$ cat >> /tmp/x
teun
vuur
$ perl -e '$_ = shift; print -M, "\n", -A, "\n", -C , "\n"' /tmp/x
5.78703703703704e-05
0.000960648148148148
5.78703703703704e-05
$ cat /tmp/x
aap
noot
mies
teun
vuur
$ perl -e '$_ = shift; print -M, "\n", -A, "\n", -C , "\n"' /tmp/x
0.000428240740740741
3.47222222222222e-05
0.000428240740740741

Perl help:

-M Script start time minus file modification time, in days.
-A Same for access time.
-C Same for inode change time (Unix, may differ for other platforms)
Brecht De Baets
Frequent Advisor

Re: How can I know that a file is being accessed ?

Thanks for the replies.

I guess I can use the method given by Hein.
I will try to write a script that runs every minute or so and looks at all the files in a certain directory.
I don't know scripting in Perl very well, so I have an additional question.
How can I loop trough all the files in a directory (for example /mnt/beelden) and check them one by one if they can be deleted ?

Best regards,
Hein van den Heuvel
Honored Contributor

Re: How can I know that a file is being accessed ?

Brecht,

Below a more complete example using perl.
Should be self explanatory

run as : perl clean.pl&
or eventually run in cron or such.

Groetjes,
Hein.


-------------------- clean.pl -------------
use warnings;
use strict;
my $stop=0;
my $total_deleted=0;
while ( ! $stop ) {
my $deleted=0;
foreach ( ) {
$stop++ if /stop/; # soft exit ?
my $modified = -M;
my $accessed = -A;
if (($modified > 1) # more than a day old?
|| ($accessed < $modified)) { # accessed after modification?
$deleted++;
unlink;
}
} # each file
$total_deleted += $deleted;
print "deleted $deleted.\n";
sleep 5 unless $stop;
} # main loop

Hein van den Heuvel
Honored Contributor
Solution

Re: How can I know that a file is being accessed ?

oops, forgot the 'retain format' check.
Hein.

------------------------ clean.pl ----------
use warnings;
use strict;
my $stop=0;
my $total_deleted=0;
while ( ! $stop ) {
my $deleted=0;
foreach ( ) {
$stop++ if /stop/; # soft exit
my $modified = -M;
my $accessed = -A;
if (($modified > 1) # more than a day old?
|| ($accessed < $modified)) { # accessed after modification?
$deleted++;
unlink;
}
} # each file
$total_deleted += $deleted;
print "deleted $deleted.\n";
sleep 5 unless $stop;
} # main loop
Brecht De Baets
Frequent Advisor

Re: How can I know that a file is being accessed ?

Thanks,
this solves my case.

Best regards,