The Linux command-line has many powerful tools for viewing and managing running processes on your UNIX/Linux machine. The lsof
command is a very useful command, it will display a list of all open files owned by active processes running on your system. Below is an excerpt from the output this command will give you.
root@deusexmachina:/home/neo# lsof | tail -n 20 lsof 26891 root 4r DIR 0,3 0 630773 /proc/26891/fd lsof 26891 root 5w FIFO 0,8 0t0 630778 pipe lsof 26891 root 6r FIFO 0,8 0t0 630779 pipe tail 26892 root cwd DIR 8,53 4096 651521 /home/neo tail 26892 root rtd DIR 8,49 4096 2 / tail 26892 root txt REG 8,49 61864 4072509 /usr/bin/tail tail 26892 root mem REG 8,49 1583120 3407895 /lib/x86_64-linux-gnu/libc-2.13.so tail 26892 root mem REG 8,49 136936 3407917 /lib/x86_64-linux-gnu/ld-2.13.so tail 26892 root mem REG 8,49 1534592 4079033 /usr/lib/locale/locale-archive tail 26892 root 0r FIFO 0,8 0t0 628464 pipe tail 26892 root 1u CHR 136,1 0t0 4 /dev/pts/1 tail 26892 root 2u CHR 136,1 0t0 4 /dev/pts/1 lsof 26893 root cwd DIR 8,53 4096 651521 /home/neo lsof 26893 root rtd DIR 8,49 4096 2 / lsof 26893 root txt REG 8,49 124976 4068930 /usr/bin/lsof lsof 26893 root mem REG 8,49 1583120 3407895 /lib/x86_64-linux-gnu/libc-2.13.so lsof 26893 root mem REG 8,49 136936 3407917 /lib/x86_64-linux-gnu/ld-2.13.so lsof 26893 root mem REG 8,49 1534592 4079033 /usr/lib/locale/locale-archive lsof 26893 root 4r FIFO 0,8 0t0 630778 pipe lsof 26893 root 7w FIFO 0,8 0t0 630779 pipe |
if you are having problems with umounting a partition or a hung process that is accessing a file, then this command will help you out. Once you find out the PID of the offending process you may then kill it with the kill -9
command to free up the file.
The ps command is also a good way to view a list of running processes. Use the ps aux command to view a complete list of all running processes. The pstree command will show a tree view of running processes. An example below, you use the pstree $LOGNAME command to view all running processes belonging to your user.
neo@deusexmachina:~/Documents$ pstree neo bash───banshee───20*[{banshee}] chrome─┬─chrome ├─chrome───2*[{chrome}] └─33*[{chrome}] chrome-sandbox───chrome─┬─7*[chrome───3*[{chrome}]] ├─chrome───4*[{chrome}] ├─chrome───2*[{chrome}] └─nacl_helper_boo dbus-daemon dbus-launch dconf-service───2*[{dconf-service}] gconfd-2 gnome-settings-───2*[{gnome-settings-}] gvfs-afc-volume───{gvfs-afc-volume} gvfs-gdu-volume gvfs-gphoto2-vo gvfsd gvfsd-http───2*[{gvfsd-http}] gvfsd-metadata gvfsd-trash lxsession─┬─lxpanel ├─openbox ├─pcmanfm ├─polkit-gnome-au───{polkit-gnome-au} ├─ssh-agent └─xscreensaver lxterminal─┬─bash ├─bash─┬─pstree │ └─xclock ├─gnome-pty-helpe └─{lxterminal} me-tv───{me-tv} menu-cached notification-da───{notification-da} pulseaudio─┬─gconf-helper └─3*[{pulseaudio}] |
As you can see, the lxsession is the master process and the lxpanel, openbox and pcmanfm processes are running under that process. As with the lxterminal command, there are processes that were executed within that terminal, therefore they are under the lxterminal process.
The ps axjf
command will also print out a process tree of running processes. Searching for a running process is easy, the grep command is used here.
neo@deusexmachina:~/Documents$ ps aux | grep bash neo 25554 0.0 0.0 20556 3368 pts/0 Ss+ Apr16 0:00 /bin/bash neo 26555 0.0 0.0 10748 1480 ? S 10:31 0:00 bash /usr/bin/banshee --redirect-log --play-enqueued neo 26830 0.0 0.0 20580 3512 pts/1 Ss 10:44 0:00 /bin/bash neo 27346 0.0 0.0 20556 3380 pts/2 Ss 11:27 0:00 /bin/bash neo 27430 0.0 0.0 7796 864 pts/2 S+ 11:33 0:00 grep bash |
This enables you to quickly find the PID of a certain process and kill it.