Operating System - HP-UX
1754708 Members
5171 Online
108823 Solutions
New Discussion юеВ

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

 
SOLVED
Go to solution
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,