View processes

If you start a program, a process ID (PID) is assigned to the process and a corresponding directory is created in /proc

In Bash, you can find the PID of the current shell with the command:

echo $$

You can also find the PID of the parent shell:

echo $PPID

To find the PID of a program on your filesystem, the utility pidof is available, for instance:

pidof ssh

If you want to use pidof to find running scripts, including the PID of the shell that is used by a script, you have to add the -s parameter.

Let's have a look into the proc directory of the current shell:

You can see all the properties of this process. To name a few:

If you execute cat environ, the output is difficult to read, because the end-of-line character is \0 instead of \n. You can fix this using the tr command to translate the \0 into \n:

cat /proc/$$/environmen | tr "\0" "\n"

The proc directory is very interesting for troubleshooting, but there are also many tools that use this information to produce a more human-friendly output. One of these utilities is the ps command. There is something strange with this command; it supports three different types of parameter: 

Output formatting for the three styles are not the same, but you can modify the behavior with options. A comparison is as follows:

The processes between square brackets are kernel processes.

You can query for specific values, for instance:

ps -q $$ -o comm

This is the same as:

cat /proc/$$/cmdline

Another utility that can help you search for a process is pgrep. It greps on values such as the name and user and shows the PID by default. The output can be formatted using parameters such as -l to list the process name, or -o to add the full command to the output.

An interactive way to monitor processes uses the top command:

The values in the columns for a process visible in top are the same as in ps. In the man-page of top you can find a good explanation of what they mean. Some of them will be covered in later chapters.

The top command or the more fancier htop command can help you to quickly identify processes taking too much memory or CPU and send a signal to the process. If you want detailed and advanced process monitoring and troubleshooting it's better to use the tooling available in Azure. This is covered in Chapter 12, Monitoring Your Workload in Azure.