Fuser alternative OR running fuser on a script


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users Fuser alternative OR running fuser on a script
# 1  
Old 07-05-2014
Fuser alternative OR running fuser on a script

Hi,

Not sure whether there is a fuser alternative or any better way to check for file in use or not.

I am wanting to check whether files are in use or not before removing them. Using fuser, the awk seems to be giving me 'weird' output not to mention that it is giving me 2 lines instead of one.

Code:
$: fuser /db/oltp/index/oltp_dwh_ind_fact_prs_phlegfi201202_01.dbf
/db/oltp/index/oltp_dwh_ind_fact_prs_phlegfi201202_01.dbf:     3559o    3157o    3040o    2923o   28148o
$: fuser /db/oltp/index/oltp_dwh_ind_fact_prs_phlegfi201202_01.dbf | awk '{ print NF }'
/db/oltp/index/oltp_dwh_ind_fact_prs_phlegfi201202_01.dbf: ooooo
5
$: fuser /db/oltp/index/oltp_dwh_ind_fact_prs_phlegfi201202_01.dbf | awk '{ print $1 }'
/db/oltp/index/oltp_dwh_ind_fact_prs_phlegfi201202_01.dbf: ooooo
3559

.

I decided to write a script below that re-directs the output of fuser and then using awk to check for field2=$2 is zero/null, if it is then the file is not in use, otherwise it is in use and should no be removed.

Currently the script is as below:

Code:
#!/bin/ksh

ls -1tr /db/oltp/data/oltp_dwh_dat_fact_prs_ph*.dbf | grep -v 2014 > check_files.tmp.00
echo
echo "Running fuser of data ..."
while read data
do
   fuser $data 1>z.out 2>&1

   if [[ -z "`awk '{ print $2 }' z.out`" ]] ; then
      echo "$data = not in use"
   else
      echo "$data = file in use"
   fi
done < check_files.tmp.00

I just want some feedback on whether this is the best solution for checking file in user using fuser before removing.

The link below show a neat trick with fuser,

Code:
http://www.c0t0d0s0.org/archives/4228-Less-known-Solaris-Features-fuser.html

ps -o pid,args -p "$(fuser /mnt/application 2>/dev/null)"

but I am not really interested on what process is in use of the file. Also, it gives an error if the file is not in use unless I do a 2>/dev/null on the ps.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Unable to get pid from fuser

bash-3.2$ fuser -f /bin/nohup.out /bin/nohup.out: 13136o 13111o The pid is 13136. Can you tell me how can i extract just the pid 13136 from the above output ? bash-3.2$ uname -a SunOS mymac 5.10 Generic_150400-26 sun4v sparc sun4v I was trying on this lines but i get strange... (3 Replies)
Discussion started by: mohtashims
3 Replies

2. UNIX for Dummies Questions & Answers

fuser on aix vs. linux

Pls. advise why on AIX the exit status is always '0' and on Linux it is always '1' even if the I just created a new file. I'm thinking if there is a background process monitoring the created 'file1' on AIX even if it is just a dummy or new file. How can I resolve this. (I need to justify that... (1 Reply)
Discussion started by: budz26
1 Replies

3. UNIX for Dummies Questions & Answers

fuser

if filename.txt is in used, exit, else continue. i tried to use fuser -c filename.txt, but returned bunch out PIDs eventhough filename.txt is not in used. any idea ? (10 Replies)
Discussion started by: tjmannonline
10 Replies

4. Solaris

FUSER problems

Greetings, I need help understanding why FUSER will not bring back PSID's on mounted filesystems. Is this a common error? Thanks in advance for your feedback. (11 Replies)
Discussion started by: Harleyrci
11 Replies

5. Shell Programming and Scripting

fuser exit code different when script is run from cron

Hi, I am writing a bash script (running on Centos 5.4) to process video (.MTS) files which may have appeared in a certain directory. The files will be dragged and dropped there from a Windows box using Samba, and the script is to check periodically (i.e. run from cron) whether any new .MTS... (0 Replies)
Discussion started by: Palamedes
0 Replies

6. UNIX for Dummies Questions & Answers

fuser

Anyone ever use fuser, i tried this command fuser /database.bk but it only returns datbase.bk: I read some of the forums online, one of them said when he used fuser, it broke down the box, i really don't want that happen. I thought fuser is to see who is accessing that file, right? any... (8 Replies)
Discussion started by: adrianlearnpro
8 Replies

7. Shell Programming and Scripting

help needed in fuser command

Hi all, I want to know if fuser command can be used to check if a file is being written or not??? Thanks In Advance Anju (1 Reply)
Discussion started by: anju
1 Replies

8. UNIX for Advanced & Expert Users

fuser ?? Look at the script !!

#SCRIPT TO CHECK WHO HAS ACCESSED THE LOG/FILE IN PAST 'N' MINUTES, AND MAIL ACCORDINGLY. MYPATH="/clocal/mqbrkrs/user/mqsiadm/sanjay/" MAIL_RECIPIENTS="abc@xyz.com" Subject="File accessed in last few minutes" >tempmail.txt >tempfind.txt find "$MYPATH" -type f -amin -1 > tempfind.txt cat... (0 Replies)
Discussion started by: varungupta
0 Replies

9. AIX

fuser brought down the box

I issued fuser -k -x -u ./amqcc_r It brought down the whole box The unix admin says if I had issued fuser -k -x -u amqcc_r then it would have not brouht down the box. Does it make sense to you guys (5 Replies)
Discussion started by: bandaru
5 Replies

10. UNIX for Dummies Questions & Answers

Help with fuser

fuser is used to check whether a file is in use by a process or not. I was putting some information in a file via a background process and was doing a cat to see the contents. It gave me the pid of background process followed by stop. Understood only half, stopped because it was writing on it... (11 Replies)
Discussion started by: vibhor_agarwali
11 Replies
Login or Register to Ask a Question