UNIX Hints & Hacks |
|||||||||||||||||||||||||||||||||||||
Chapter 1: Topics in Administration |
|
||||||||||||||||||||||||||||||||||||
|
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.
Flavors: AT&T, BSD
Shells: All
Syntax:
/usr/sbin/fuser files
Command:
/usr/sbin/fuser /bin/csh
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/csh /bin/csh: 1485t 1106t
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 csh processes, execute the following simple command:
# fuser -k /bin/csh /bin/csh: 1485t 1106t
This replaces the following set of commands you would use a number of times throughout the day:
# ps -ef | grep csh
root 1484 1485 1 17:54:02 pts/1 0:00 /bin/csh root 1116 1117 1 17:54:16 pts/1 0:00 grep csh root 1090 1091 0 Aug 09 pts/2 0:00 /bin/csh
# kill 1484 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
Line 1: Define the shell to use.
Lines 3-6: Find the process of the file running and kill its process.
Using fuser is simple, to the point, and very efficient. It can be time consuming to pick from hundreds of processes on larger servers that might or might not relate to the process you are trying to kill or gather information on. This single command quickly gathers information and kills the PID, if necessary, on request. It is a very useful command for an administrator.
I have become accustomed to using this command for killing predetermined processes. I have several scripts similar to the one described in place to kill off various user applications, X sessions, and shells, among other things. On a remote system defined as a trusted host, it is nice to be able to execute a remote shell and kill processes quickly without having to log in to the remote machine. To the user it appears as though you have killed processes without even logging in to the system: it's magic to them!
Man pages:
fuser, kill, ps
UNIX Hints & Hacks |
|||||||||||||||||||||||||||||||||||||
Chapter 1: Topics in Administration |
|
||||||||||||||||||||||||||||||||||||
|
© Copyright Macmillan USA. All rights reserved.