Skip to main content

View Running Processes from the Command Line

Hands-On Lab

 

Photo of Michael Christian

Michael Christian

Course Development Director in Content

Length

00:45:00

Difficulty

Beginner

In this hands-on lab, we will practice finding and viewing processes running on a Linux system. Being able to locate a particular process and view its status is a fundamental component of systems administration.

What are Hands-On Labs?

Hands-On Labs are scenario-based learning environments where learners can practice without consequences. Don't compromise a system or waste money on expensive downloads. Practice real-world skills without the real-world risk, no assembly required.

View Running Processes from the Command Line

Introduction

In this hands-on lab, we will practice finding and viewing processes running on a Linux system. Being able to locate a particular process and view its status is a fundamental component of systems administration.

Connecting to the Lab

  1. Open your terminal application, and run the following command (remember to replace PUBLIC_IP with the public IP you were provided on the lab instructions page):
    ssh cloud_user@PUBLIC_IP
  2. Enter yes at the prompt.
  3. Enter your cloud_user password at the prompt.

Determine How Many Processes Are Currently Running

  1. Run the following command:
    ps aux | grep -v grep | wc -l

Determine the Current System Load

  1. Run the following command:
    cat /proc/loadavg

Determine How Many Processes Are Running as cloud_user

  1. Run the following command:
    ps -U cloud_user | wc -l

Determine the PID of the xfce4-session Process

  1. Run the following command:
    ps aux | grep xfce4-session | grep -v grep

Determine How Many Threads the xfce4-session Process Is Using

  1. View the current threads reported in the PID's status.
    cat /proc/PID_FROM_ABOVE/status | grep Threads

Write a Small Shell Script that Returns the Number of Threads a Process Is Running

  1. Make a new directory named bin.
    mkdir bin
  2. Create the file threads.sh in the bin directory.
    vim bin/threads.sh
  3. In the vim text editor, enter the following:

    #!/bin/bash
    
    if [ -n $1 ]
    then
    _pid=$(ps aux | grep -E "$1$" | grep -v grep | grep -v threads.sh | awk '{print $2}')
    cat /proc/$_pid/status | grep Threads
    fi
  4. Press Esc to exit Insert mode, and type :wq to save and exit the file.
  5. Make the file executable.
    chmod u+x bin/threads.sh
  6. Use the script we just created to determine the number of threads the xfce4-session process is running.
    ./bin/threads.sh xfce4-session

Conclusion

Congratulations, you've successfully completed this hands-on lab!