Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to determine if a file is open 1

Status
Not open for further replies.

Xitron

MIS
Aug 12, 2002
32
US
AIX 4.3.3.0-10 - Oracle 8.1.7.4

I'm trying to write a bourne script to periodically backup the Oracle archive redo files to a different server just to cover my arse. However, I don't want to make the mistake of copying a file that is in the process of being written. Is there a simple AIX command-line method for determining if a file is open, and therefore should not be copied?

I'm thinking of something along the lines of:

for fil in `ls archive*`
do
<test to see if file is open, and if so, set a variable>
if <variable in previous step not set>
cp $fil backup-location
else
mail -s &quot;$fil was open and was not copied&quot; me@my.com
fi
done

Ideas?

Thanks,

Unca Xitron
 
Hi,

fuser <file_name> gives you PID that access a file (man fuser for more details).
 
Hi nwardez. First, thank you for replying. fuser is a great option, but either it is not doing what we'd hoped, or I'm not interpreting the output correctly.

Example:

On one screen I do the following:

# vi myfile.sh

On a second screen I then run the following command, and get the following:

# fuser myfile.sh; echo $?
myfile:
0
#

And if I `touch` a new file, for the sole purpose of testing:

# touch crapmonkey
# fuser crapmonkey; echo $?
crapmonkey:
0
#

So in the case of a file that is definitely open for editing, vs. a file that has been newly created and which is NOT open, the output and the $? are identical. I don't have access to the AIX man pages (why oh why, but that's a different thread altogether), so I peeked at the Linux man page for same. Very different. In fact, when I intentionally err in the AIX version, the only options I am offered are:

Usage: fuser [-cdfkuVx] File ...

Not being lazy here, folks. I just don't have access to the docs I need. I'll post THAT thread in a minute.

Any clarifications on the fuser idea, or any other ideas?

Unca Xitron
 
Yes, my answer was a bit short...

fuser shows you the PID of the process that access a file. It returns 0 if it runs successfully. That's why you get 0 with &quot;echo $?&quot; after fuser. But if you try:

1. First screen
touch foo
tail -f foo
2. Second screen
fuser foo

2nd screen will give you a process number you can &quot;ps|grep&quot; to check it is. Hope it helps.

By the way, did you check your MANPATH environment variable for accessing man pages ?
 
Thanks, nwardez. I guess I figured that having a file open with vi would be sufficient for having it considered &quot;open&quot;. So would I be correct in guessing that a file that is being written to would show up with a PID that I could grep for as a test for being &quot;open&quot;?

As for the man pages, I found several excellent posts (thanks aixqueen!) regarding the man pages. Looks like it is time for me to dig out the CDs. :-/

Thanks again,

Unca Xitron

 
Try something like this in your script:

CNT=`fuser $fil 2>&1 | awk '$2 != &quot;&quot; { print $1 }' | cut -d: -f1|wc -l`
if [ $CNT -eq 0 ];then
cp blabla
else
mail blabla
fi
IBM Certified Confused - MQSeries
IBM Certified Flabbergasted - AIX 5 pSeries System Administration
 
Thanks aixmurderer! Since I've never taken the time to learn awk (BAD sysadmin!! Bad!!!), this was my approach...

for fil in $ARCHDIR/arch*
do
INUSE=`fuser $fil | cut -d: -f2` # Get a PID if in use

if [ $INUSE > &quot;&quot; ] # Is file being written to?
then
echo &quot;$fil in use... not backed up&quot; >> $LOGFILE #skipit
else
grep $fil $DONELIST # Has it already been backed up?
ISDONE=$?

if [ $ISDONE -ne 0 ]
then
echo $fil >> $BACKLIST # Add to list to back up
fi

fi

done


Clumsy, and not as elegant as many scripters on this site could have done, but it works. :) I'm studying perl and awk at this time, so my next attempt should be a lot cleaner.

Thanks again, all. Great place this website!

Unca Xitron
 
Thought I would jump in and explain the confusion about using vi to edit a file and fuser to see if the file being edited is open. The problem is that vi does not keep the file open during the editing session. It creates a copy of the file and uses that for the updates. When you leave vi and save changes it overwrites the original file.


 
mgshn: thanks for clearing that up for me. Much appreciated!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top