logo
  • Home
  • Test & Rank
  • About
  • Training
  • Programming
  • CS
  • IT
  • IS
  • ECE
  • EEE
  • EE
  • Civil
  • Mechanical
  • Chemical
  • Metallurgy
  • Instrumentation
  • Aeronautical
  • Aerospace
  • Biotechnology
  • Mining
  • Marine
  • Agriculture
  • MCA
  • BCA
  • Internship
  • Jobs
  • Contact

Questions & Answers

C Interview Questions
C++ Questions
Linux MCQs
C# Quiz
Java MCQs
JavaScript MCQs
SAN Questions
PHP Questions
Python Quiz

Computer Science Questions

Operating System Quiz
Computer Architecture MCQs
Software Architecture MCQs
Software Engineering MCQs
Artificial Intelligence MCQs
LISP Programming MCQs
Database Management MCQs
Computer Network MCQs
Microprocessor MCQs

C Programming Examples

Simple C Programs
C - Arrays
C - Matrix
C - Strings
C - Bitwise Operations
C - Linked Lists
C - Stacks & Queues
C - Searching & Sorting
C - Trees
C - Strings
C - File Handling
C - Mathematical Functions
C - Puzzles & Games
C Programs - Recursion
C Programs - No Recursion

Java Algorithms

Java - Numerical Problems
Java - Combinatorial Problems
Java - Graph Problems
Java - Hard Graph Problems
Java - Computation Geometry
Java - Sets & Strings
Java - Data-Structures
Java - Collection API Problems

C++ Algorithms

C++ - Numerical Problems
C++ - Combinatorial Problems
C++ - Graph Problems
C++ - Hard Graph Problems
C++ - Computation Geometry
C++ - Sets & Strings
C++ - Data-Structures
C++ - STL Library

C Algorithms

C - Numerical Problems
C - Combinatorial Problems
C - Graph Problems
C - Hard Graph Problems
C - Computation Geometry
C - Sets & Strings
C - Data-Structures

« Prev Page
Next Page »

5+ “fuser” Command Usage Examples in Linux

Posted on March 2, 2014 by Manish

This tutorial explains Linux “fuser” command, options and its usage with examples.

fuser – identify processes using files or sockets.

DESCRIPTION

advertisement

fuser displays the PIDs of processes using the specified files or file systems.
In the default display mode, each file name is followed by a letter denoting the type of access:

c- current directory.
e- executable being run.
f- open file. f is omitted in default display mode.
F- open file for writing. F is omitted in default display mode.
r- root directory.
m- mmap’ed file or shared library.

fuser returns a non-zero return code if none of the specified files is accessed or in case of a fatal error. If at least one access has been found, fuser returns zero.

SYNOPSIS

fuser [-a|-s] [-4|-6] [-n space] [-signal] [-kimuv] name …
fuser -l
fuser -V

OPTIONS :

-a
Show all files specified on the command line. By default, only files that are accessed by at least one process are shown.
-k
Kill processes accessing the file. Unless changed with -signal, SIGKILL is sent. An fuser process never kills itself, but may kill other fuser processes. The effective user ID of the process executing fuser is set to its real user ID before attempting to kill.
-i
Ask the user for confirmation before killing a process. This option is silently ignored if -k is not present too.
-l
List all known signal names.
-m
name specifies a file on a mounted file system or a block device that is mounted. All processes accessing files on that file system are listed. If a directory file is specified, it is automatically changed to name/. to use any file system that might be mounted on that directory.
-n space
Select a different name space. The name spaces file (file names, the default), udp (local UDP ports), and tcp (local TCP ports) are supported. For ports, either the port number or the symbolic name can be specified. If there is no ambiguity, the shortcut notation name/space (e.g. name/proto) can be used.
-s
Silent operation. -u and -v are ignored in this mode. -a must not be used with -s.
-signal
Use the specified signal instead of SIGKILL when killing processes. Signals can be specified either by name (e.g. -HUP) or by number (e.g. -1).
-u
Append the user name of the process owner to each PID.
-v
Verbose mode. Processes are shown in a ps-like style. The fields PID, USER and COMMAND are similar to ps. ACCESS shows how the process accesses the file. If the access is by the kernel (e.g. in the case of a mount point, a swap file, etc.), kernel is shown instead of the PID.
-V
Display version information.
-4
Search only for IPv4 sockets. This option must not be used with the -6 option and only has an effect with the tcp and udp namespaces.
-6
Search only for IPv6 sockets. This option must not be used with the -4 option and only has an effect with the tcp and udp namespaces.
–
Reset all options and set the signal back to SIGKILL.

EXAMPLES

advertisement

1. Display every process ID that is using the current directory (./)

$ fuser .

2. For verbose output use -v

$ fuser -v .
                     USER   PID ACCESS COMMAND
.:                   abc   1562 ..c.. gnome-session
                     abc   1620 ..c.. metacity
                     abc   1626 ..c.. polkit-gnome-au
                     abc   1627 ..c.. nm-applet
                     abc   1629 ..c.. gnome-power-man
                     abc   1630 ..c.. mintUpdate
                     abc   1631 ..c.. nautilus
                     abc   1632 ..c.. gnome-panel
                     abc   1633 ..c.. bluetooth-apple
                     abc   1655 ..c.. python
                     abc   1697 ..c.. syndaemon
                     abc   1777 ..c.. python
                     abc   1824 ..c.. gnome-terminal
                     abc   1852 ..c.. firefox
                     abc   1886 ..c.. plugin-containe
                     abc   2380 ..c.. bash
                     abc   2464 ..c.. vlc

3. Check processes using an executable

I opted for running fuser on firefox executable which is running on my system :

$ ps -aef | grep firefox
himanshu  1852     1  7 06:57 ?        00:06:06 /usr/lib/firefox-11.0/firefox
himanshu  1886  1852 14 06:58 ?        00:12:10 /usr/lib/firefox-11.0/plugin-container /usr/lib/flashplugin-installer/libflashplayer.so -greomni /usr/lib/firefox-11.0/omni.ja 1852 true plugin
himanshu  2551  2380  0 08:24 pts/1    00:00:00 grep --colour=auto firefox

So the first entry in the output above gives the path to firefox executable running in the system. I’ll use the same path in the following fuser command

advertisement
$ fuser /usr/lib/firefox-11.0/firefox
/usr/lib/firefox-11.0/firefox:  1852e

So we see that the output is the PID of the process (which can be cross checked against the output of the ‘ps’ command above). The post fix ‘e’ signifies that the file is an executable.

4. To output process owner use -u

An extra information regrading the user name of the process owner can also be appended to each PID.

$ fuser -uv .
                     USER   PID ACCESS COMMAND
.:                   abc   2380 ..c.. (abc)bash
                     abc   2855 ..c.. (abc)gcalctool

5. Display information about all files on command line

$ fuser  ./Desktop/ ./Downloads/
./Desktop/:           1892c  1894c

If we wish to have information on all the files we supplied as input then -a option can be used.

$ fuser -a ./Desktop/ ./Downloads/
./Desktop/:           1892c  1894c
./Downloads/:

6. Check Processes Using TCP/UDP Sockets

socket_serv is a C program executable which is a TCP server that listens on a particular port(say 5000 in this case).

$ ./socket_serv

Since the above stated socket_serv sample C program executable is running on TCP port 5000,

$ fuser -v -n tcp 5000
                     USER   PID   ACCESS COMMAND
5000/tcp:            abc   4311   F....  socket_serv

7. Kill Processes that are Using a particular Program

We saw that a TCP server is running on the system which is accessing the binary file ‘socket_serv’. Now, lets try to kill the process using this binary file using fuser.

$ fuser -v -k socket_serv
                     USER   PID ACCESS COMMAND
socket_serv:         abc   4311 ...e.   socket_serv

Notice that we have used the ‘-k’ flag to kill the process using file ‘socket_serv’. Lets see on the other terminal where the server was running.

$ ./socket_serv
Killed

Note : In the above example we saw that the flag ‘-k’ is used when we want to kill the processes using a particular file but to avoid the killing of processes accidentally, another option ‘-i’ exists. If we use this option then ‘fuser’ will run in interactive mode and will ask before killing the process.

8. List all signals supported by fuser

$ 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

9. Sending specific signal to a process

The following sends SIGHUP instead of SIGKILL, when you use the -k option.

$ fuser -v  -k -HUP -i ./

Sanfoundry Global Education & Learning Series – 1000 Linux Tutorials.

If you wish to look at all Linux commands and their usage examples, go to Linux Commands Tutorial.
« Prev Page - “renice” Command Usage Examples in Linux
» Next Page - “wall” Command Usage in Linux

« “renice” Command Usage Examples in Linux
“wall” Command Usage in Linux »
advertisement

Deep Dive @ Sanfoundry:

  1. C Programming Examples
  2. C# Basic Programming Examples
  3. C# Programming Examples on LINQ
  4. C# Programming Examples
  5. C# Programming Examples on Networking
  6. Linux Questions and Answers
  7. C# Programming Examples on Threads
  8. C# Programming Examples on Files
  9. C Programming Examples on File Handling
  10. Linux Command Tutorials with Examples and Explanations
Manish Bhojasia
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer & SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage, Advanced C Programming, SAN Storage Technologies, SCSI Internals & Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him @ LinkedIn | Facebook | Twitter

Best Careers

Developer Tracks
SAN Developer
Linux Kernel Developer
Linux Driver Developer
Linux Network Developer

Live Training Photos
Mentoring
Software Productivity
GDB Assignment
Sanfoundry is No. 1 choice for Deep Hands-ON Trainings in SAN, Linux & C, Kernel Programming. Our Founder has trained employees of almost all Top Companies in India such as VMware, Citrix, Oracle, Motorola, Ericsson, Aricent, HP, Intuit, Microsoft, Cisco, SAP Labs, Siemens, Symantec, Redhat, Chelsio, Cavium, ST-Micro, Samsung, LG-Soft, Wipro, TCS, HCL, IBM, Accenture, HSBC, Mphasis, Tata-Elxsi, Tata VSNL, Mindtree, Cognizant and Startups.

Best Trainings

SAN I - Technology
SAN II - Admin
Linux Fundamentals
Advanced C Training
Linux-C Debugging
System Programming
Network Programming
Linux Threads
Kernel Programming
Kernel Debugging
Linux Device Drivers

Best Reference Books

Computer Science Books
Algorithm & Programming Books
Electronics Engineering Books
Electrical Engineering Books
Chemical Engineering Books
Civil Engineering Books
Mechanical Engineering Books
Industrial Engineering Books
Instrumentation Engg Books
Metallurgical Engineering Books
All Stream Best Books

Questions and Answers

1000 C Questions & Answers
1000 C++ Questions & Answers
1000 C# Questions & Answers
1000 Java Questions & Answers
1000 Linux Questions & Answers
1000 Python Questions
1000 PHP Questions & Answers
1000 Hadoop Questions
Cloud Computing Questions
Computer Science Questions
All Stream Questions & Answers

India Internships

Computer Science Internships
Instrumentation Internships
Electronics Internships
Electrical Internships
Mechanical Internships
Industrial Internships
Systems Internships
Chemical Internships
Civil Internships
IT Internships
All Stream Internships

About Sanfoundry

About Us
Copyright
Terms
Privacy Policy
Jobs
Bangalore Training
Online Training
Developers Track
Mentoring Sessions
Contact Us
Sitemap
© 2011 Sanfoundry. All Rights Reserved.