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

Check to see if a file is open by any process 1

Status
Not open for further replies.

mstrong

Programmer
Sep 27, 2001
16
0
0
US
I'm helping out on a project that uses Cron to go out and get a file, which is then processed through another program. The current setup has a script that tests to see if a file is there, but it may still be open and being written to. We're looking for a way to find out if the file is open (or not open for that matter). Any thoughts?
 
Try the fuser command.

The man page for fuser states:
fuser Command

Purpose

Identifies processes using a file or file structure.

Syntax

fuser [ -c | -d | -f ] [ -k ] [ -u ] [ -x ] [ -V ]File ...

Description

The fuser command lists the process numbers of local processes that use the local or remote files specified by the File parameter. For block special devices, the command lists the processes that use any file on that device.

Each process number is followed by a letter indicating how the process uses the file:

c Uses the file as the current directory.

e Uses the file as a program's executable object.

r Uses the file as the root directory.

s Uses the file as a shared library (or other loadable object).

The process numbers are written to standard output in a line with spaces between process numbers. A new line character is written to standard error after the last output for each file operand. All other output is written to standard error.

Flags

-c Reports on any open files in the file system containing File.

-d Implies the use of the -c and -x flags. Reports on any open files which have been unlinked from the file system (deleted from the parent directory). When used in conjunction with the -V flag, it also reports the inode number and size of the deleted file.

-f Reports on open instances of File only.

-k Sends the SIGKILL signal to each local process. Only the root user can kill aprocess of another user.

-u Provides the login name for local processes in parentheses after the process number.

-V Provides verbose output.

-x Used in conjunction with -c or -f, reports on executable and loadable objects in addition to the standard fuser output.


So you may not need to write something more than a wrapper around fuser for the file in question. I hope this helps. Einstein47
(Love is like PI - natural, irrational, endless, and very important.)
 
I'll pass both of those on. Thanks for the advice.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top