using fuser Instead of ps

Here is an alternative way to get the process ID (PID) of a particular process. The fuser command is more reliable and can be quicker than ps.

/usr/sbin/fuser files

/usr/sbin/fuser /bin/bash

The fuser command outputs the PIDs of all processes that are currently opened under the named file. If a named directory is passed through fuser, the PIDs of all the processes that have a file or files open for reading in that directory are displayed. The files passed must be fully qualified in order for the command to function properly. If they are not, the proper syntax is displayed on standard output.
There is one caveat to using this command. You must have read access to /dev/kmem and /dev/mem. This is because fuser takes an actual snapshot of the system image that is found in these character devices at the time it is executed.

# fuser /bin/bash
/bin/bash: 1034t1106t

The t at the end of the each PID denotes that these processes have their own executable text segment that is open.
The fuser command has an option ( -k) that can be passed to send a kill signal to the PID. So, to kill all the bash processes, execute the following simple command:

# fuser -k /bin/bash
/bin/bash: 1034t 1106t

This replaces the following set of commands you would use a number of times throughout the day:
# ps -ef | grep bash

root 1033 1034 1 17:54:02 pts/1 0:00 /bin/bash
root 1216 1217 1 17:54:16 pts/1 0:00 grep bash
root 1090 1091 0 Aug 09 pts/2 0:00 /bin/bash
# kill 1033 1090

If multiple processes are associated with a particular process that you run within your environment, you can easily write a script to kill the application and all the daemons associated with it. Suppose an application lives in /sbin called bsr. It has several daemons that run independently from bsr, such as bsrqqd, bsrexecd, and bsrojbd. You can write a quick-and-dirty script to kill the entire application by using fuser:

#! /bin/sh
fuser -k /sbin/bsr
fuser -k /sbin/bsrqqd
fuser -k /sbin/bsrexecd
fuser -k /sbin/bsrojbd

Leave a Reply

Your email address will not be published. Required fields are marked *

CAPTCHA * Time limit is exhausted. Please reload the CAPTCHA.

This site uses Akismet to reduce spam. Learn how your comment data is processed.