close open files before remove


 
Thread Tools Search this Thread
Operating Systems Solaris close open files before remove
# 1  
Old 09-03-2007
Question close open files before remove

Hi
I have a script to remove log files when it reaches %70 usage in order to descending order of dates. But sometimes it happens to remove open - being processes files , so makes them unlinked from the directors and results them disappeared.
How can I solve this problem , simple scenario I thought that first close file and then remove. But is there any unix utility to do this??

For example: I looked for file-close option in "rm" command, but I could not find. ?

Any advise ?
# 2  
Old 09-03-2007
I thing something wrong with your script. It doesn't seem as rm problem
# 3  
Old 09-03-2007
script

here is my code:

Code:
#!/usr/bin/sh
dir=/cbmdata/00/gdd

 function ()
 {
   while [ 1 ] 
   do
   df -k | grep /cbmdata/00/gdd | tr -d '%' | \
   read a b c d e other
   line=`find /cbmdata/00/gdd -name "LOGS*" |sort -nr |tail -1`

   if ![ -s $line ]
   then
    break
    elif (( "$e" >= "$ref" ))
     then
      rm -f $line
       else
        exit 0
  fi
done
   
 }

  for file in $dir/*
  do
     [ -s /cbmdata/00/gdd/* ] && function $file
  done

p.s. If I restart the related process hidden file becomes visible,this is why I called the rpoblem like that.
# 4  
Old 09-03-2007
first of all I didn't like an infinete loop and remove in a function. I thing same thing become to me before. rm is a bit slower so it can do next line of code before finishing remove.
Are you sure that this code is running why ;

df -k | grep /cbmdata/00/gdd | tr -d '%' | \
read a b c d e other /*does nothing must be **/

e=`df -k |grep $dir |awk '{print $4}' |tr -d '%'`

what is ref no input in the code.I think you mean %70 or else

where is the ascending order of date ? You must change all code

for example

dir=/cbmdata/00/gdd
function ()
{

if [ `df -k |grep $dir |awk '{print $4}' |tr -d '%'` -gt $ref ]
then
rm -f $file
fi
}

ref=$1
for file in $dir/*
do
[ -s $dir/* ] && function $file
done

if you mean LOG01 is older date than LOG02 ?
than I will write that code tomorrow. I must live now

Last edited by fazliturk; 09-03-2007 at 12:10 PM..
# 5  
Old 09-03-2007
When you do find a file that matches your criteria, first do a fuser -fu on the file to check if any process is using it. If yes, you can let it alone, or if the contents of the file are not required, just truncate the file (using >).
# 6  
Old 09-04-2007
I sent the wrong code, sorry to trouble you:

correct one (working one is that)

Code:
#!/usr/bin/ksh
ref=90

while [ 1 ] 
do
  df -k | grep /cbmdata/00/gdd | tr -d '%' | \
  read a b c d e other
  if (( "$e" >= "$ref" ))
  then
    line=`find /cbmdata/00/gdd -name "LOGS*" |sort -nr |tail -1`
    # echo $line
    rm -f $line
  else
    exit 0
  fi
done

# 7  
Old 09-04-2007
Question

Quote:
Originally Posted by blowtorch
When you do find a file that matches your criteria, first do a fuser -fu on the file to check if any process is using it. If yes, you can let it alone, or if the contents of the file are not required, just truncate the file (using >).
blowtorch

yes , file contents is not so important, truncate means closing this file ?
How can I do it? does it belong fuser as option?
, could you please give me an example
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to get value from a close and open parenthesis?

Hi Gurus, I have an input like the one below. What i wanted to achieved is to create a select statement based from that information INPUTInsert into table_name (col1,col2,col3,col4,col5,DATE1,DATE2,col6,col7,col8,col9,col10,col11) values (6752,14932156,24,'ALL','Staff',to_date('04/17/2017... (6 Replies)
Discussion started by: ernesto
6 Replies

2. Shell Programming and Scripting

Work with setsid to open a session and close it correctly

I would like to create the following script: run a python script with setsid python may or may not fail with exception check if all of the group processes were terminated correctly if not, kill the remaining processes How can I do that? Thanks a lot (3 Replies)
Discussion started by: ASF Studio
3 Replies

3. Shell Programming and Scripting

Need to delete large set of files (i.e) close to 100K from a directory based on the input file

Hi all, I need a script to delete a large set of files from a directory under / based on an input file and want to redirect errors into separate file. I have already prepared a list of files in the input file. Kndly help me. Thanks, Prash (36 Replies)
Discussion started by: prash358
36 Replies

4. IP Networking

Open/close of ports

Hi, I have read some forum theads about the open and close ports. some points are clear and it is not working on my machine or something am i missing? I have commented out a port /etc/services, one application uses then when i use the telnet <hostname> <port_blocked> it shows connected..... (1 Reply)
Discussion started by: balamv
1 Replies

5. Programming

when parent process close, how to close the child?

can someone provide an example, where if the parent process quits for any reason, then the child process will also close? (3 Replies)
Discussion started by: omega666
3 Replies

6. Solaris

file open/read/write/close/access by process

Hi want to know what file (descriptor+filename+socket) is being accessed by particular process on solaris. Purpose : while running perf. test, needs to find where is the bottleneck. We are providing concurrnet load for around 1 hr and needs to capture data related to file usage pattern... (1 Reply)
Discussion started by: raxitsheth
1 Replies

7. Windows & DOS: Issues & Discussions

Can DOS close an open file?

I'm trying to setup a cron job for my brother that goes out to the web and retrieves an excel file and overwrites the existing copy on his desktop. The problem I'm facing is I have to kill the process (excel.exe) if the file is open while the batch file runs, otherwise, it will create another copy... (2 Replies)
Discussion started by: quattro20v
2 Replies

8. UNIX for Advanced & Expert Users

Number of open files (Dir's) how to close?

I am having a client/server application which will download files from server. If server has directories, it will create directories/sub directories and then download files. In this process, I observed that number of open files are more than 400 (which is approxmately same as number of dir/subdir... (1 Reply)
Discussion started by: satyam90
1 Replies

9. UNIX for Dummies Questions & Answers

Should a UNIX daemon process close open fds?

I have a UNIX daemon process that's been started by a parent process, an application server. The behavior of this daemon process is to inherit and use the app server's file descriptors (ports/sockets). When I shutdown the app server, the daemon continues to run, because there may be other... (1 Reply)
Discussion started by: kunalashar
1 Replies

10. UNIX for Dummies Questions & Answers

File status - open/close ??

Is there any commands in UNIX, like isopen(),isclose() to know whether a file has been opened for read/write purpose.? Thanks (3 Replies)
Discussion started by: Pal
3 Replies
Login or Register to Ask a Question