The fuser utility in Linux is a powerful tool. As the name suggests it gives information about file user or the process that is currently using the file or directory.
But fuser functionality is not just limited to giving information about the process. The article explains how to use fuser utility with 5 practical examples.
1. Who is Using a File or Directory?
This is the basic use of fuser command. i.e to Identify which processes are using a particular file or directory.
$ fuser . ./: 3965c 4175c 4281c 4334c 4337c
In the above example we used the fuser utility to find all the processes using the current directory ‘./’ .
We see that the output consists of process IDs of the processes using fuser but all the PIDs are followed by a character ‘c’. This indicates the type of access. The type of access can be any one of the following:
- 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
So ‘c’ in the output would mean that these processes are using this directory as their current directory.
Use Option -v to display detailed information in the output:
$ fuser -v ./ USER PID ACCESS COMMAND ./: himanshu 3965 ..c.. bash himanshu 4175 ..c.. gedit himanshu 4281 ..c.. bash himanshu 4334 ..c.. socket_serv himanshu 4337 ..c.. bash
So we see above that running fuser on the current directory gives the information on all the processes that are using this directory.
2. fuser on an Executable
socket_serv is a C program executable which is a TCP server that listens on a particular port.
When you execute fuser on this executable, you’ll see the following:
$ fuser -v socket_serv USER PID ACCESS COMMAND socket_serv: himanshu 4334 ...e. socket_serv
The access specifier in this example is ‘e’. Its different from the access specifier we saw in above examples. It conveys that the file is an executable.
3. Check Processes Using TCP/UDP Sockets
Using fuser we can also check the processes using TCP/UDP sockets. Since the above stated socket_serv sample C program executable is running on TCP port 5000, lets use fuser utility on this socket.
$ fuser -v -n tcp 5000 USER PID ACCESS COMMAND 5000/tcp: himanshu 4334 F.... socket_serv
So we see that fuser gives all detailed information of the process running on TCP port 5000.
Other than the examples above, we can use the ‘-m’ flag with this utility to display processes using a mounted file system like a USB drive.
4. Kill Processes that are Using a particular Program
Till now we learned that fuser provides information on the processes using files, directories, sockets etc. But the power of this utility is not restricted to providing information only. You can also kill processes using this utility.
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: himanshu 4334 ...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
We already explained you how you can kill a process using 4 different methods. Now you know one more method to kill a process.
5. Interactively Kill Processes using fuser
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. See the example below.
$ fuser -v -k -i socket_serv USER PID ACCESS COMMAND socket_serv: himanshu 5643 ...e. socket_serv Kill process 5643 ? (y/N) y
So we see that using ‘-k’ and ‘-i’ we can selectively kill processes using a particular file.
Now, this is a very powerful use of ‘fuser’ command.
Suppose you want to delete a file forcefully but it is being used by many processes then the processes won’t let you delete the file. In that case, you can use fuser utility to kill all the processes (or selected processes) that are using that file.
$ fuser -v -k -i ./ USER PID ACCESS COMMAND ./: himanshu 3965 ..c.. bash himanshu 4175 ..c.. gedit himanshu 4281 ..c.. bash himanshu 4334 ..c.. socket_serv himanshu 4337 ..c.. bash Kill process 3965 ? (y/N) y Kill process 4175 ? (y/N) y Kill process 4281 ? (y/N) y Kill process 4334 ? (y/N) y Kill process 4337 ? (y/N) y
Note that the use of ‘-k’ sends a SIGKILL signal to the processes using that particular file. We can use the option -SIGNAL to send any other signal. The list of signals supported by fuser is given by:
$ 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
For example, the following sends SIGHUP instead of SIGKILL, when you use the -k option.
$ fuser -v -k -HUP -i ./