Understanding Processes in Linux

In other words, it’s an instance of a command or application that you have executed using the terminal or graphical user interface (GUI).

To view all active processes on your system, use the ‘ps’ command followed by the ‘aux’ option:


# This script uses the 'ps' command to view all active processes on the system.
# The 'aux' option is used to display all processes from all users.

# The '$' symbol indicates that the following command should be executed in the terminal.

# The 'ps' command is used to report a snapshot of the current processes.
# The 'aux' option specifies the format of the output to include all processes.

$ ps aux

This will display a list of all currently running processes along with their respective PID (process ID), username, and resource usage. The output may look something like this:

# This script displays a list of all currently running processes along with their respective PID (process ID), username, and resource usage.

# The output may look something like this:

# USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
# root         1  0.0  0.2 396840 25720 ?        Ss   Mar05   0:00 /sbin/init
# ...

# The following code uses the `ps` command to retrieve information about running processes and pipes it to the `awk` command for formatting.

ps aux | awk '{
    # The `ps` command with the `aux` option displays all processes for all users in a user-oriented format.
    # The output is then piped to the `awk` command for further processing.

    # The `awk` command uses the `print` function to display the desired fields from the `ps` output.
    # The fields are separated by spaces and can be accessed using the `$` symbol followed by the field number.

    # The first line of the `ps` output contains the column headers, so it is skipped using the `NR` variable.
    if (NR > 1) {
        # The `printf` function is used to format the output in a specific way.
        # The `%10s` format specifier ensures that the field is displayed in a 10-character wide column.
        # The `%5.1f` format specifier ensures that the field is displayed as a floating point number with one decimal place.
        # The `%6.1f` format specifier ensures that the field is displayed as a floating point number with one decimal place.
        # The `%8s` format specifier ensures that the field is displayed in a 8-character wide column.
        # The `%s` format specifier ensures that the field is displayed as a string.
        printf("%10s %6s %5s %6s %6s %8s %8s %8s %s\n", $1, $2, $3, $4, $5, $6, $7, $8, $11);
    }
}'

# The output of the `awk` command is then displayed, showing the desired fields in a formatted table.

Let’s break down the output:
– USER: The username that initiated the process.
– PID: A unique identifier for each running process. This number is assigned by the kernel and cannot be changed.
– %CPU: The percentage of CPU time used by this process since it was started or last resumed.
– %MEM: The amount of memory (RAM) that this process is currently using, expressed as a percentage of total available RAM.
– VSZ: The size of the virtual memory image for this process in kilobytes. This includes both code and data segments.
– RSS: The resident set size, which is the amount of physical memory (RAM) that this process has currently allocated.
– TTY: The terminal or device from which this process was started. If it’s ‘?’ then it means that the process does not have a controlling terminal.
– STAT: The current state of the process. This can be one of several values, such as R (running), S (sleeping), D (uninterruptible sleep), and Z (zombie).
– START: The time when this process was started or last resumed.
– TIME: The total amount of CPU time used by this process since it was started or last resumed, expressed in minutes and seconds.
– COMMAND: The command that initiated the process. This can be useful for identifying which program is using up resources on your system.

To view processes owned by a specific user, use ‘ps aux’ followed by the username you want to filter by:


# This script uses the 'ps' command to view processes owned by a specific user.
# The 'aux' option displays all processes, including those owned by other users.
# The output is then piped to the 'grep' command, which filters the results based on the given username.

# The '$' symbol indicates that this is a command to be executed in the terminal.

# The 'ps' command displays information about active processes on the system.
# The 'aux' option specifies the format of the output to include all processes.

# The '|' symbol is used to pipe the output of one command to another.

# The 'grep' command searches for a specific pattern in the given input.
# In this case, it will filter the output of 'ps aux' to only include processes owned by the user 'jdoe'.

# The 'jdoe' in the command is the username we want to filter by.

# The final result will be a list of processes owned by the user 'jdoe'.

This will display all active processes that are currently running under the ‘jdoe’ account.

If you need more information about a particular process, such as its command line arguments or environment variables, use the ‘pgrep’ and ‘ps’ commands together:


# This script uses the 'pgrep' and 'ps' commands to display all active processes 
# that are currently running under the 'jdoe' account. It also provides additional 
# information about a specific process, such as its command line arguments and 
# environment variables.

# The 'pgrep' command searches for processes based on their name or other attributes.
# In this case, it searches for processes with the name 'firefox' and returns their 
# process IDs (PIDs).

# The '&&' operator allows for the execution of multiple commands in a single line. 
# In this case, the 'ps' command will only be executed if the 'pgrep' command is 
# successful.

# The '-o' flag for the 'ps' command specifies the output format. In this case, 
# 'pid,args' will display the PID and command line arguments for each process.

# The '--no-headers' flag for the 'ps' command removes the header from the output.

# The '$()' syntax allows for the output of one command to be used as an argument 
# for another command. In this case, the output of 'pgrep firefox' will be used as 
# the argument for the 'ps' command.

# Overall, this script will display the PID and command line arguments for all 
# active processes with the name 'firefox' that are running under the 'jdoe' account.

$ pgrep -u jdoe firefox && ps -o pid,args --no-headers $(pgrep -u jdoe firefox)

This will display the PID (process ID) and command line arguments for any currently running instances of Firefox.

To kill a process by its PID, use ‘kill’ followed by the PID:


# This script kills a process by its PID (process ID)
# It takes in the PID as an argument and uses the 'kill' command to terminate the process

# First, we need to get the PID of the process we want to kill
# We can use the 'ps' command to list all currently running processes
# We then use 'grep' to filter the results and only show processes with 'firefox' in their name
# Finally, we use 'awk' to extract the PID from the results and store it in a variable called 'PID'
PID=$(ps aux | grep firefox | awk '{print $2}')

# Now that we have the PID, we can use the 'kill' command to terminate the process
# We use the '-9' flag to force the process to terminate
# We pass in the PID variable as an argument to the 'kill' command
kill -9 $PID

# Note: It is important to use the '-9' flag when killing a process as it ensures the process is terminated immediately
# If we do not use this flag, the process may continue running in the background and cause issues later on
# Also, it is important to note that this script will only work for processes with 'firefox' in their name
# If we want to kill a process with a different name, we would need to modify the 'grep' command accordingly
# Additionally, this script assumes that there is only one instance of Firefox running

# Overall, this script is a simple and quick way to kill a process by its PID without having to manually find and terminate it.

Be careful when using this command as it can have unintended consequences if you accidentally kill an important system process or another user’s process.

SICORPS