Bootstrap FreeKB - Linux Commands - fuser (determine the PID of a file)
Linux Commands - fuser (determine the PID of a file)

Updated:   |  Linux Commands articles

The fuser command can be used to determine the PID of a certain open file, network connection, or process. The fuser command without any options will display the options that the fuser command can accept.

 


PID of open files

The -v or --verbose option displays verbose output. In this example, all of the open files in the /root directory are displayed. There is only one open file, the bash shell, with PID 4905. "..c.." means current directory.

[root@server1 `]# fuser -v /root
          USER  PID   ACCESS COMMAND
/root:    root  4905  ..c..  bash

 

The -u or --user option can be used to add the user to the command column.

[root@server1 `]# fuser -v /root
          USER  PID   ACCESS COMMAND
/root:    root  4905  ..c..  (root)bash

 

The -m or --mount option can be used to view the PIDs being used on a certain device. For example, if a USB flash drive is mounted to /dev/sda3:

[root@server1 `]# fuser -m "/dev/sda3"
/dev/sda3:    10177 10184 10185

 


PID of network connections

The -n or --namespace option can be used to determine the PIDs associated with a network port. In this example, the port 22 (SSH) is using three processes, 2337, 8810, and 8815.

[root@server1 `]# fuser -n tcp 22
22/tcp:        2337  8810  8815

 

The -v or --verbose option can be used to gather more details on each process.

[root@server1 `]# fuser -v -n tcp 22
               USER     PID  ACCESS  COMMAND
22/tcp:        root     2337 F....   sshd
               root     8810 F....   sshd
               User1    8815 F....   sshd

 


Kill open file

Let's say user john.doe is using the less command to read the file /home/john.doe/example.file.

[root@server1 `]# fuser -v /home/john.doe
                USER      PID   ACCESS COMMAND
/home/john.doe: john.doe  5010  ..c..  less

 

The -k or --kill option can be used to kill the process.

[root@server1 `]# fuser -k /home/john.doe/example.file
/home/john.doe/example.file: 5010

 

In john.doe terminal, message "killed" will be displayed.

example.file (END)killed
[john.doe@server1 ~]#

 

By default, the -k or --kill option will use the kill signal, SIGKILL. The -l or --list-signals option can be used to list the available signals.

[root@server1 `]# fuser -l
HUP INT QUIT ILL TRAP ABRT IOT BUS FPE KILL USR1 SEGV USR2 PIPE ALRM TERM
STKFLT CHLD CONT STOP ISTP TTIN TTOU URG XCPU XFSZ VTALRM PROF WINCH IO PWR SYS
UNUSED

 

The -SIGNAL option can be used to use the preferred kill signal (replace SIGNAL with the prefer signal). In this example, the hang up (HUP) signal is used.

[root@server1 `]# fuser -k -HUP /home/john.doe/example.file
/home/john.doe/example.file: 5046

 

In john.doe terminal, message "hangup" will be displayed.

example.file (END)hangup
[john.doe@server1 ~]#

 

To proceed with caution, the -i or --interactive option can be used to display a prompt before issuing a kill signal.

[root@server1 `]# fuser -k -i /home/john.doe/example.file
Kill process 5099? (y/N)

 


Kill network connection

Let's say john.doe has made an SSH connection to the server.

[root@server1 `]# fuser -v -n tcp 22
               USER     PID  ACCESS  COMMAND
22/tcp:        john.doe 8815 F....   sshd

 

The -k or --kill and -n or --namespace option can be used to kill a network connection.

[root@server1 `]# fuser -k -n tcp /home/john.doe/sshd

 

In john.doe shell, a message such as Server unexpectedly closed network connection will be displayed, and john.doe will lose SSH connection to the server.

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter af42b8 in the box below so that we can be sure you are a human.