In this section, we ll look at a few of the GNU/Linux commands that work with the previously mentioned API functions. We ll look below at commands that permit us to inspect the process list and send a signal to a process or to an entire process group .
The ps command provides a snapshot in time of the current set of processes active on a given system. The ps command takes a large variety of options; we ll explore a few here.
In the simplest form, we can simply type ps at the keyboard to see a subset of the processes that are active:
$ ps PID TTY TIME CMD 22001 pts/0 00:00:00 bash 22186 pts/0 00:00:00 ps $
First, we see our bash session (our own process) and our ps command process (every command in GNU/Linux is executed within its own subprocess). We could see all of the processes running using the -a option (list shortened for brevity):
$ ps -a PID TTY TIME CMD 1 ? 00:00:05 init 2 ? 00:00:00 keventd 3 ? 00:00:00 kapmd 4 ? 00:00:00 ksoftirqd_CPU0 ... 22001 pts/0 00:00:00 bash 22074 ? 00:00:00 sendmail 22189 pts/0 00:00:00 ps $
In this example, we see a number of other processes including the mother-of-all-processes ( init , process ID 1) and assorted kernel threads. If we wanted to see only those processes that were associated with our user , we could accomplish this with the ”User option:
$ ps User mtj PID TTY TIME CMD 22000 ? 00:00:00 sshd 22001 pts/0 00:00:00 bash 22190 pts/0 00:00:00 ps $
Another very useful option is -H , which tells us the process hierarchy. In the next example, we ll request all processes for user mtj but then also request their hierarchy (parent/child relationships):
$ ps User mtj -H PID TTY TIME CMD 22000 ? 00:00:00 sshd 22001 pts/0 00:00:00 bash 22206 pts/0 00:00:00 ps #
Here we see that our base process is an sshd session (since we re connected to this server via the secure shell). This is the parent of my bash session, which in turn is the parent of the ps command that we just executed.
The ps command can be very useful, especially when we re interested in finding our process identifiers to kill a process or send it a signal.
The top command is related to ps , but top runs in real time and lists the activity of the processes for the given CPU. In addition to the process list, we can also see statistics about the CPU (number of processes, number of zombies , memory used, and so on). We re obviously in need of a memory upgrade here (only 4MB free). This sample list has again been shortened for brevity.
19:27:49 up 79 days, 10:04, 2 users, load average: 0.00, 0.00, 0.00 47 processes: 44 sleeping, 3 running, 0 zombie, 0 stopped CPU states: 0.0% user 0.1% system 0.0% nice 0.0% iowait 99.8% idle Mem: 124984k av, 120892k used, 4092k free, 0k shrd, 52572k buff 79408k actv, 4k in_d, 860k in_c Swap: 257032k av, 5208k used, 251824k free 37452k cached PID USER PRI NI SIZE RSS SHARE STAT %CPU %MEM TIME CPU COMMAND 22226 mtj 15 0 1132 1132 868 R 0.1 0.9 0:00 0 top 1 root 15 0 100 76 52 S 0.0 0.0 0:05 0 init 2 root 15 0 0 0 0 SW 0.0 0.0 0:00 0 keventd 3 root 15 0 0 0 0 RW 0.0 0.0 0:00 0 kapmd 4 root 34 19 0 0 0 SWN 0.0 0.0 0:00 0 ksoftirqd_CPU0 ... 1708 root 15 0 196 4 0 S 0.0 0.0 0:00 0 login 1709 root 15 0 284 4 0 S 0.0 0.0 0:00 0 bash 22001 mtj 15 0 1512 1512 1148 S 0.0 1.2 0:00 0 bash
The rate of sampling can also be adjusted for top , in addition to a number of other options (see the top man page for more details).
The kill command, like the kill API function, allows us to send a signal to a process. We can also use it to list the signals that are relevant for the given processor architecture. For example, if we d like to see the signals that are available for the given processor, we d use the -l option:
# kill -l 1) SIGHUP 2) SIGINT 3) SIGQUIT 4) SIGILL 5) SIGTRAP 6) SIGABRT 7) SIGBUS 8) SIGFPE 9) SIGKILL 10) SIGUSR1 11) SIGSEGV 12) SIGUSR2 13) SIGPIPE 14) SIGALRM 15) SIGTERM 17) SIGCHLD 18) SIGCONT 19) SIGSTOP 20) SIGTSTP 21) SIGTTIN 22) SIGTTOU 23) SIGURG 24) SIGXCPU 25) SIGXFSZ 26) SIGVTALRM 27) SIGPROF 28) SIGWINCH 29) SIGIO 30) SIGPWR 31) SIGSYS 33) SIGRTMIN 34) SIGRTMIN+1 35) SIGRTMIN+2 36) SIGRTMIN+3 37) SIGRTMIN+4 38) SIGRTMIN+5 39) SIGRTMIN+6 40) SIGRTMIN+7 41) SIGRTMIN+8 42) SIGRTMIN+9 43) SIGRTMIN+10 44) SIGRTMIN+11 45) SIGRTMIN+12 46) SIGRTMIN+13 47) SIGRTMIN+14 48) SIGRTMIN+15 49) SIGRTMAX-14 50) SIGRTMAX-13 51) SIGRTMAX-12 52) SIGRTMAX-11 53) SIGRTMAX-10 54) SIGRTMAX-9 55) SIGRTMAX-8 56) SIGRTMAX-7 57) SIGRTMAX-6 58) SIGRTMAX-5 59) SIGRTMAX-4 60) SIGRTMAX-3 61) SIGRTMAX-2 62) SIGRTMAX-1 63) SIGRTMAX #
For a running process, we could send a signal as follows . In this example, we ll send the SIGSTOP signal to the process identified by the pid 23000 .
# kill -s SIGSTOP 23000
This places the process in the STOPPED state (not running). We could start the process up again by giving it the SIGCONT signal, as:
# kill -s SIGCONT 23000
Like the kill API function, we can signal an entire process group by providing a pid of 0. Similarly, all processes within the process group can be sent a signal by sending the negative of the process group.