The ps Command
The ps
command on a Debian system supports both BSD and SystemV features and helps to identify process activity statically.
Style
Style | Description |
---|---|
BSD | Supports BSD-style options and output. |
System V | Supports SystemV-style options and output. |
Typical Command
ps aux
ps -efH
Features
Feature | Description |
---|---|
Display %CPU %MEM | Displays the CPU usage and memory usage of processes. |
Display PPID | Displays the parent process ID (PPID) of a process. |
Table 9.10: List of ps Command Styles
For zombie (defunct) child processes, you can kill them by their parent process ID identified in the “PPID” field.
The top Command
The top
command on a Debian system has rich features and helps to identify what process is acting funny dynamically. It is an interactive full-screen program. You can get its usage help by pressing the “h”-key and terminate it by pressing the “q”-key.
Listing Files Opened by a Process
You can list all files opened by a process with a process ID (PID), e.g., 1, using the following command:
$ sudo lsof -p 1
The PID=1 is usually the init program.
Tracing Program Activities
You can trace program activity with strace
, ltrace
, or xtrace
for system calls and signals, library calls, or communication between X11 client and server. You can trace system calls of the ls
command as follows:
$ sudo strace ls
Tip: Use the strace-graph
script found in /usr/share/doc/strace/examples/
to make a nice tree view.
Identification of Processes using Files or Sockets
You can identify processes using files by fuser
, e.g., for /var/log/mail.log
:
$ sudo fuser -v /var/log/mail.log
This displays the process ID (PID) and command name of the process that has an open file descriptor to the specified file.
Repeating a Command with a Constant Interval
The watch
command executes a program repeatedly with a constant interval while showing its output in full-screen mode. For example:
$ watch w
This displays who is logged on to the system updated every 2 seconds.
Repeating a Command Looping Over Files
There are several ways to repeat a command looping over files matching some condition, e.g., matching glob pattern “*.ext”:
- Shell for-loop method (see Section 12.1.4):
for x in *.ext; do if [ -f "$x" ]; then command "$x"; fi; done
find
andxargs
combination:
find . -type f -maxdepth 1 -name '*.ext' -print0 | xargs -0 -n 1 command
find
with-exec
option with a command:
find . -type f -maxdepth 1 -name '*.ext' -exec command {} \;
find
with-exec
option with a short shell script:
find . -type f -maxdepth 1 -name '*.ext' -exec sh -c "command {} && echo 'successful'" \;
The above examples are written to ensure proper handling of funny file names such as ones containing spaces. See Section 10.1.5 for more advanced uses of find
.