Lots of logs to move....don't remove if open


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Lots of logs to move....don't remove if open
# 1  
Old 12-17-2009
Lots of logs to move....don't remove if open

I have a ksh script that currently moves a day's worth of log files (about 15,000) files to a different directory. The issue is that about 100 of these files are still open for write when this happens. I need an efficient way to ensure that these files aren't open without doing an lsof on each individual file within my loop. I think I need to get the list of open files once, and then check my outer loop against this list or array, but I'm not quite sure how to script that. Any assistance?
Thanks
# 2  
Old 12-17-2009
Call lsof once, save its output, and check its output for the files you want. You could also craft a fixed list of files to avoid. Or simply send the services that refuse to give up their log files a signal to reopen them after.
# 3  
Old 12-17-2009
Thank you. How do I take the saved output of file names and then compare them to the inputs of every file in the directory? I'd like to do something like

for every file in directory
do
if file not in (array or list of open files)
move it
done
# 4  
Old 12-19-2009
Code:
for  file in /path/dir/*
do
  lsof | grep "$file"
  if [ $? -eq 1 ];then
      echo "$file safe to remove"
  if
done

# 5  
Old 12-19-2009
You can also check using 'fuser' command (fuser <filename>)
# 6  
Old 12-21-2009
How can I collect this post for future use without reply ?

https://www.unix.com/shell-programmin...remove if open

Last edited by rdcwayx; 12-21-2009 at 06:50 PM..
# 7  
Old 12-21-2009
Quote:
Originally Posted by rdcwayx
How can I collect this post for future use without reply ?
Code:
[URLremove=https://www.unix.com/shell-programming-scripting/126241-lots-logs-move-dont-remove-if-open.html]Lots of logs to move....don't remove if open[/URL]

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to remove answers and explanation parts from a text file with lots of questions.?

Hi, I have a text file with thousands of questions in it. Each question (multiple lines) with multiple choice options, Answer and Explanation (optional). I need to delete Answer & explanation parts for all Questions and insert a blank line before net question. Each question starts with NO. I... (4 Replies)
Discussion started by: prvnrk
4 Replies

2. Shell Programming and Scripting

If I ran perl script again,old logs should move with today date and new logs should generate.

Appreciate help for the below issue. Im using below code.....I dont want to attach the logs when I ran the perl twice...I just want to take backup with today date and generate new logs...What I need to do for the below scirpt.............. 1)if logs exist it should move the logs with extention... (1 Reply)
Discussion started by: Sanjeev G
1 Replies

3. Shell Programming and Scripting

To check if a file is open and in use (logs are being written to it)

Hello Experts, I need to write a shell script to check if a file is open and something is being written to it. I want to know how OS handles it. I checked with lsof command but it is not working. For a test I did this. while true; do echo `date` >>abc.txt; done then I checked lsof |... (5 Replies)
Discussion started by: shekhar_4_u
5 Replies

4. Shell Programming and Scripting

Remove words from file2 that don't exist in file1

Hi I have to list of words file1 and file2, I want to compare both lists and remove from file2 all the words that don't exist in file1. How can I do this? Many thanks (4 Replies)
Discussion started by: noliveira
4 Replies

5. Solaris

Open Terminal Don't work

Hi, I installed solaris 10 x86 on my local system. it was working fine. today when i started the system, it started up without any problem. when i tried to open the terminal it didn't open any terminal. Plz help me (0 Replies)
Discussion started by: malikshahid85
0 Replies

6. Programming

problem with lots of open sockets

I'm programming in C with lots of sockets, (asynchronous handling), and for a while it goes ok, but after a while it starts to hang, or not get complete connections anymore. checking my router, i see that i get nat tables overflow and even my DNS cacher seems to be having serious issues... ... (4 Replies)
Discussion started by: alien999999999
4 Replies

7. Shell Programming and Scripting

Script to move files based on Pattern don't work

Hi people, i need you help on this if you can. I have a script that does the move command when it searches for a that doesn't match the pattern. This Pattern is on a list. When it run's and doesn´t found no files but it haves 2 more folders it moves the folders too. Ex:... (1 Reply)
Discussion started by: osramos
1 Replies

8. Shell Programming and Scripting

script to move logs

Does anyone have a good script to move log files from a cron? There are a lot of logs, about 100 or more and they are constantly being written too. (7 Replies)
Discussion started by: photon
7 Replies

9. UNIX for Advanced & Expert Users

How to find one partucular user logs when there are lots of users running on it

On my application there are lots of users are doing there work or tasks? ...In my SSH or in 'Putty' i am observing logs? Hot to observe one particular 'user' logs.. even through there are lots of users working on it? For EX: i am log in with use rid:nikhil@in.com. another one log in with... (4 Replies)
Discussion started by: ksr.test
4 Replies
Login or Register to Ask a Question