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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

close all open files 2

Status
Not open for further replies.

scriptfinder

Technical User
Apr 17, 2006
18
US
Hi!

Can someone help me find a way to close all open files, there could be some exceptions though, by a shell script, before I start a certain process?

Thanks in advance...

 
You can't close ALL open files. At the very least the OS needs a lot of files open to work.

Why do you need to do this? If it's to back up application logs or something like that, usually bringing down the application will free up the logs. If it's a file used by a lot of users, log them all off. If it's for doing backups, do it in single user mode.

If you just want to make sure all files in a given directory are closed, meaning there is no process running with it open, this would do it...
Code:
#!/bin/ksh

DIR=/path/to/files

# Do a nice kill to let them close their files

fuser * 2>/dev/null | while read PID; do kill ${PID}; done

sleep 10

# Do a nasty kill for those that didn't leave

fuser * 2>/dev/null | while read PID; do kill -9 ${PID}; done
Something like that would work, but that could be a lot of killin'!
 
I guess we don't really need the "[tt]while[/tt]" loop...
Code:
#!/bin/ksh

DIR=/path/to/files

# Do a nice kill to let them close their files

fuser * 2>/dev/null | xargs -n1 kill ${PID}

sleep 10

# Do a nasty kill for those that didn't leave

fuser * 2>/dev/null | xargs -n1 kill -9 ${PID}

If you just want to see which processes have files open in your directory, this'll do...
Code:
fuser * 2>/dev/null | xargs -n1 ps -f -p | grep -v STIME
 
Oops, here's a bug fix (call it v1.1)...
Code:
#!/bin/ksh

DIR=/path/to/files

# Do a nice kill to let them close their files

fuser [b]${DIR}/[/b]* 2>/dev/null | xargs -n1 kill ${PID}

sleep 10

# Do a nasty kill for those that didn't leave

fuser [b]${DIR}/[/b]* 2>/dev/null | xargs -n1 kill -9 ${PID}
 
Hi

My [tt]fuser[/tt] ( fuser (psmisc) 20.1 ) can kill the processes itself. As far as I know, this is a general [tt]fuser[/tt] feature, but please correct me if not.
man fuser said:
-k Kill processes accessing the file. Unless changed
with -signal, SIGKILL is sent.
Code:
fuser -k .

Feherke.
 
Hey, Cool! Learn something new every day!

I guess I should check the man pages myself.

[bigsmile]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top