psmisc: a closer look to a standard package
January 13th, 2008 edited by TinchoArticle submitted by Adrian von Bidder. And guess what? We still need you to submit good articles about software you like!
The psmisc package is probably installed on almost all Debian and Ubuntu installations and contains a number of small tools related to process management on Unix systems. Namely, these are pstree
, killall
, fuser
and peekfd
. Below follows a short description of these useful tools. While everybody certainly knows killall
and probably pstree
, the package also contains fuser
and peekfd
, which were new to me until very recently (though I hear at least fuser
is an age-old tool. Shame on me.)
killall
is exactly as evil as it sounds: it kills all processes with a given name. SIGTERM
is used by default, but it can of course use any other signal. It also has options to match processes by user, by a specified regex or by process group. I usually use it to shoot parts of KDE with invocations like:
$ killall kio_imap4
Since kmail seems to have something of a troubled relationship with my IMAP server. (As an aside, I’d also like to point out the program slay
, packaged in the slay package, which simply kills all processes belonging to a specified user.)
A nice overview on what is running on a system is the output of pstree
. Part of the output on my system is:
$ pstree init─┬─NetworkManager───2*[{NetworkManager}] ├─NetworkManagerD ├─acpid ├─hald───hald-runner─┬─hald-addon-acpi │ ├─hald-addon-cpuf │ ├─hald-addon-dell │ ├─hald-addon-inpu │ └─hald-addon-stor ├─kdeinit─┬─artsd │ ├─2*[kio_file] │ ├─kio_http │ ├─3*[kio_imap4] │ ├─kioexec───wfica.sh───wfica │ ├─klauncher │ ├─konqueror │ ├─konsole───bash─┬─pstree │ │ └─xchat───{xchat} │ └─kwin ├─kdesktop───lineakd───2*[{lineakd}] ├─kdm─┬─Xorg │ └─kdm───startkde───kwrapper └─sshd
Options to pstree
include display of PIDs, users and SE-Linux contexts.
fuser
lets you find out what processes use a certain file, for example:
$ fuser -v /home/avbidder/.xsession-errors USER PID ACCESS COMMAND /home/avbidder/.xsession-errors: avbidder 4409 F.... startkde avbidder 4533 F.... kdeinit avbidder 4536 F.... dcopserver avbidder 4538 F.... klauncher avbidder 4540 F.... kded avbidder 4547 F.... kwrapper avbidder 4549 F.... ksmserver avbidder 4550 F.... kwin avbidder 4552 F.... kdesktop avbidder 4554 F.... kicker avbidder 4562 F.... artsd avbidder 4567 F.... kmix avbidder 4568 F.... konsole avbidder 4572 F.... beagled avbidder 4574 F.... lineakd avbidder 4579 F.... knetworkmanager avbidder 4680 F.... knotify avbidder 4916 F.... kio_uiserver avbidder 5706 F.... akregator avbidder 5708 F.... kttsd avbidder 5742 F.... kio_file avbidder 5864 F.... beagled-helper avbidder 6939 F.... konqueror avbidder 7076 F.... konqueror avbidder 7185 F.... kmail avbidder 7696 F.... kio_imap4
fuser
also has options to send signals to these programs, so you can easily KILL all these programs with fuser -k /home/avbidder/.xsession-errors
(this uses SIGKILL
by default, but of course you can change this.)
While fuser
is a regular system administrator’s tool, peekfd
is a real deep-diver: it lets you watch what goes on on a filedescriptor of a process. (Please note that peekfd
is not available in etch, you need psmisc from Lenny or newer.)
If you type peekfd <pid>
at a terminal, it will follow all file descriptors of the process, to get an output like this:
$ peekfd 7808 writing fd 1: foo bar
Where 7808 is just cat >/dev/null
in another terminal window, with me typing “foo bar” in it. The manual page warns about it killing the monitored process (which I haven’t seen so far) and I’ve had peekfd
segfault on me a few times, so I guess there’s some real ugly magic going on behind the scenes.
Instead of following all input and output of a process, by specifying the file descriptor number, you can follow only a selection of the files a process has opened. The directory /proc/<pid>/fd
is a good way to find out which file descriptors might be worth looking at.
So let this article be a motivation to have a closer look at all the packages that you’ve installed on your system since at least potato and haven’t really looked at lately…
January 13th, 2008 at 11:10 am
That pstree is really nice and I discover it now! So many years wasted :)
January 13th, 2008 at 11:57 pm
I’ve been writing about Linux for money for about 3 years… I’d seen pstree mentioned but not the subcommands.
thanks
January 14th, 2008 at 1:22 pm
Caution with killall : this command behaves very differently on Linux and on Unix : it generally shutdowns the machine without any warning, and possibly without checking if there’s an argument. (see also KNOWN BUGS section in the killall(1) manpage).
January 14th, 2008 at 7:40 pm
killall can also kill all processes of a user with the –user flag.
It’s worth reading through the manual pages of those commands (generally - I try to sometimes read through manual pages of even “old” commands - you can discover many gems even in “man ls” and certainly in “man iptables”).
The standard way I use “pstree” is with “-pal”, which gives process id, command line arguments and unlimited output line length.
January 18th, 2008 at 2:32 pm
How about pgrep, skill, and snice? These all deserve mention here as well.
Here’s a great page of doc on the topic: http://tldp.org/LDP/GNU-Linux-Tools-Summary/html/controlling-processes.html
April 1st, 2008 at 3:27 pm
ps facx will also give a nice tree plus some more info you can have with ps options