Linux to see if a file is being used (read/write)

Source: Internet
Author: User

fuser Function
Fuser can show which program is currently using a file on the disk, mount point, or even network port, and give the program process details.
Fuser Displays the process ID using the specified file or file system. By default, each file name is followed by a letter that represents the access type.
The type of access is as follows:
C represents the current directory
E Use this file as the executable object of the program
F Open the file. Not displayed by default.
F Open file for write operation. Not displayed by default.
R root directory.
M mapping files or shared libraries.

s use this file as a shared library (or other loadable object)
Fuser returns nonzero when the specified file is not accessed, or when an error occurs.
In order to view processes that use TCP and UDP sockets, the-n option is required and the namespace is specified. Both the default IpV4 and IpV6 are displayed. Sockets can be local or remote ports, and remote addresses. All domains are optional, but the preceding ', ' must exist. As follows:
[Lcl_port] [, [Rmt_host][,[rmt_port]]
For both the IP address and port, the name and number representation can be used.
The fuser only outputs the PID to the standard output, and the other outputs to the standard error output.
Common Options
-a displays all the files specified on the command line, and the files that are accessed by default are displayed.
-C and-M are used for POSIX compatibility.
-K kills the process that accesses the file. If no-signal is specified, the Sigkill signal is sent.
-I asks the user before killing the process, which is ignored if there is no-k option.
-l lists all known signal names.
-m name specifies a file mounted on a file system or mounted block device (name). All processes that access this file or file system will be listed. If a directory is specified, it is automatically converted to "name/" and uses all file systems mounted under that directory.
-N space specifies a different namespace (space). Different spatial files (file name, default here), TCP (local TCP port), UDP (local UDP port) are supported here. For ports, you can specify the port number or name, and if not ambiguous, you can use the form of a simple representation, such as: Name/space (i.e., a representation of a shape such as a 80/tcp).
-s silent mode, this time-u,-v will be ignored. -A cannot be used with-S.
-signal uses the specified signal instead of Sigkill to kill the process. Signals (such as-hup,-1) can be represented by name or number, which is used with-K, otherwise it is ignored.
-U adds the user name of the process owner after each PID.
-v Verbose mode.  Outputs the output of the PS-like command, including many domains such as Pid,user,command, and if the kernel is accessed then the PID is kernel. -V Output version number.
4 Use IPV4 sockets, cannot and-61 applications, only in the case of the-n TCP and UDP name existence is not ignored.
6 use IPV6 sockets, cannot and-41 applications, only in the case of the-n TCP and UDP name existence is not ignored.
-Reset all the options to set the signal to Sigkill.
using the example

Show process information for using a file
$ fuser-um/dev/sda2
/dev/sda2:6378c (Quietheart) 6534c (Quietheart) 6628 (Quietheart)
6653C (Quietheart) 7429c (Quietheart) 7549c (Quietheart) 7608c (Quietheart)
This command is useful in umount, and can be used to find out what else is available for this device.
Kill the program that opened the Readme file

$fuser-M-k-i Readme
Here, you will be asked if you are sure before kill. It is best to add-V to know that the process will be killed.
View 80 ports for those programs that use TCP
$fuser-V-N TCP 80
Or

$fuser-V 80/tcp
application of different signals of fuser
Use the-l parameter to list fuser known signals
# fuser-l
HUP INT QUIT ILL TRAP ABRT IOT BUS FPE KILL USR1 SEGV USR2 PIPE alrm term
Stkflt chld CONT STOP tstp ttin ttou URG xcpu xfsz vtalrm PROF WINCH IO PWR SYS
UNUSED
Fuser can send it a known signal to the specified file process to access instead of the-K parameter sent by default Sigkill, for example: Just suspend the process, then send the HUP signal is ready
# fuser-v/root/install.log
User Process number Permission command
/root/install.log:root 3347 f .... tail
# fuser-k-sighup/root/install.log
/root/install.log:3347
# fuser-v/root/install.log

Reference to: Http://hi.baidu.com/bossyt/item/020e2cddf09463ee3cc2cb1a
Http://wenku.baidu.com/view/7e42df3667ec102de2bd8927.html
Http://blog.sina.com.cn/s/blog_59af21260100tv1m.html

This article turns from: http://czmmiao.iteye.com/blog/1733722 thank the original author for sharing

Linux to see if a file is being used (read/write)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.