Process management in Linux

top command to monitor resource usage in system

The top command is a Linux utility that displays real-time information about the running processes on a system. It shows the top processes sorted by CPU usage, as well as other information such as memory usage, load average, and uptime.

To run the top command, simply type top at the command prompt. This will display a list of the top processes, along with a summary of the system’s overall resource usage. The list is updated in real-time, so you can see the resource usage change as processes start and stop.

By default, the top command displays the processes in sorted order, with the most CPU-intensive processes at the top of the list. You can use the u key to filter the list by a specific user, and the k key to kill a process.

The top command also has a number of options that allow you to customize the output. For example, you can use the -p option to show only processes with a specific process ID, or the -o option to sort the list by a specific metric (such as memory usage or I/O).

You can also use the u key to filter the list by a specific user, and the k key to kill a process. To kill a process, select the process by pressing the k key, then enter the process ID and press Enter.

In addition to the basic functionality, the top command has a number of options that allow you to customize the output. Some of the most useful options include:

  • -p: Show only processes with a specific process ID.
  • -o: Sort the list by a specific metric (such as memory usage or I/O).
  • -d: Set the update interval in seconds.
  • -n: Set the number of iterations.
  • -b: Run in batch mode, which allows you to output the results to a file or pipe.

You can use these options in combination with the top command to get a more detailed view of the resource usage on your system. For example, you can use the top -o %MEM -n 1 -b command to sort the list by memory usage and display the results in batch mode.

df command to display amount of disk space

The df command in Linux is used to display the amount of disk space used and available on a file system. It is commonly used to check the available disk space on a system and ensure that there is enough free space for new files and programs.

To run the df command, simply type df at the command prompt. This will display a list of the file systems on the system, along with information about the total size, used space, available space, and percentage of usage for each file system.

By default, the df command displays the file systems in 1K blocks. You can use the -h option to display the sizes in “human-readable” format, which is easier to read for most people. For example, you can use the df -h command to display the sizes in units of kilobytes, megabytes, or gigabytes, depending on the size of the file system.

The df command can also display the file system type, the mount point, and the device name for each file system. You can use the -T option to display the file system type, and the -m option to display the mount point.

Here are a few examples of how to use the df command in Linux:

  1. Display the disk usage for all file systems:
$ df

This command displays a list of all file systems on the system, along with the total size, used space, available space, and percentage of usage for each file system.

  1. Display the disk usage in “human-readable” format:
$ df -h

This command displays the sizes in units of kilobytes, megabytes, or gigabytes, depending on the size of the file system. This can be easier to read for most people.

  1. Display the file system type and mount point:
$ df -Tm

This command displays the file system type and mount point for each file system. The file system type is the type of file system (such as ext4 or btrfs), and the mount point is the directory where the file system is mounted.

  1. Display the disk usage for a specific file system:
$ df -h /dev/sda1

This command displays the disk usage for the file system on the device /dev/sda1. You can replace /dev/sda1 with the device name or mount point of the file system you want to display.

  1. Display the inodes (index nodes) for a file system:
$ df -i

This command displays the number of inodes (index nodes) used and available on each file system. Inodes are used to store information about files and directories on a file system.

ps command to get information about the running processes

The ps command is a Linux utility that displays information about the processes running on a system. It is commonly used to list the processes that are currently running and to identify the process ID (PID) of a specific process.

To run the ps command, simply type ps at the command prompt. This will display a list of the processes that are running on the system. By default, the ps command displays a limited set of information about each process, including the PID, the command line that was used to start the process, and the terminal associated with the process.

You can use the ps command with a number of options to customize the output. For example, you can use the -aux option to show all processes, including those that are owned by other users and those that are not associated with a terminal. You can also use the -f option to show the full command line for each process, or the -u option to show the user who owns each process.

For a complete list of options and more information on using the ps command, you can consult the ps man page or use the man ps command in a terminal.

Here are a few examples of how to use the ps command:

  • ps: Display a list of the processes that are running on the system.
  • ps -aux: Display a list of all processes, including those that are owned by other users and those that are not associated with a terminal.
  • ps -f: Display the full command line for each process.
  • ps -u: Display the user who owns each process.
  • ps -p 123: Display information about the process with PID 123.

few more examples of how to use the ps command in Linux:

  1. Display information about a specific process by PID:
    ps –p 123

This command displays information about the process with PID 123. You can replace 123 with the PID of the process you want to display.

  1. Display information about all processes associated with a terminal:
    ps -t tty1

This command displays a list of all processes that are associated with terminal tty1. You can replace tty1 with the name of the terminal you want to display.

  1. Display information about processes running on a specific terminal:
    ps -C bash -t tty1
    This command displays a list of processes running on terminal tty1 that were started with the bash command. You can replace bash with the name of the command you want to display, and tty1 with the name of the terminal.
  1. Display a tree view of the process hierarchy:
    ps -ejH
    This command displays a tree view of the process hierarchy, showing the parent-child relationships between processes. This can be useful for understanding how processes are related and how they interact with each other.
  1. Display a summary of the process status:
    ps -eo user,pid,%cpu,%mem,vsz,rss,tty,stat,time,command
    This command displays a summary of the process status, including the user who owns the process, the PID, CPU and memory usage, virtual memory size, resident set size, terminal, process state, and elapsed time. This can be useful for getting a detailed view of the resource usage of processes on the system.
  1. Display a summary of the resource usage for all processes:
    ps -eo pid,%cpu,%mem,rss,vsz,command
    This command displays a summary of the resource usage for all processes, including the PID, CPU usage, memory usage, resident set size, virtual memory size, and command line.
  1. Display a summary of the resource usage for a specific user:
    ps -U username -o pid,%cpu,%mem,rss,vsz,command
    This command displays a summary of the resource usage for all processes owned by the user username. You can replace username with the name of the user you want to display.
  1. Display a summary of the resource usage for a specific process group:
    ps -g groupid -o pid,%cpu,%mem,rss,vsz,command
    This command displays a summary of the resource usage for all processes in the process group with ID groupid. You can replace groupid with the ID of the process group you want to display.

jobs command to get the list of background jobs

In Linux, the jobs command is used to display a list of the background jobs that are running or stopped in the current shell. A background job is a command that is run in the background, allowing you to continue running other commands while the job is executing.

To run a command as a background job, you can use the & symbol at the end of the command. For example:

command &

This will run the command in the background, and the shell will return the control to you immediately. You can then use the jobs command to see a list of the background jobs that are running or stopped in the current shell.

The jobs command displays a list of the background jobs, along with their job numbers and status. You can use the fg command to bring a background job to the foreground, or the bg command to resume a stopped background job.

For example, to bring the first background job to the foreground, you can use the following command:

$ fg %1

To resume a stopped background job, you can use the bg command followed by the job number. For example:

$ bg %2

This will resume the second stopped background job in the background.

For more information on the jobs command and how to use it, you can consult the jobs man page or use the man jobs command in a terminal.

fg command to bring a job to foreground

the fg command is used to bring a background job to the foreground. A background job is a command that is run in the background, allowing you to continue running other commands while the job is executing.

To bring a background job to the foreground, you can use the fg command followed by the job number. The job number is the number assigned to the job when it was started. You can use the jobs command to see a list of the background jobs that are running or stopped in the current shell, along with their job numbers.

For example, to bring the first background job to the foreground, you can use the following command:

$ fg %1

This will bring the first background job to the foreground, allowing you to interact with it as if it were a normal foreground job.

You can also use the fg command with the % symbol followed by the name of the command that was used to start the background job. For example:

fg %command

This will bring the background job that was started with the command to the foreground.

The fg command is often used in combination with the jobs and bg commands. The jobs command displays a list of the background jobs, and the bg command is used to resume a stopped background job in the background.

bg command to resume a job in background

the bg command is used to resume a stopped background job in the background. A background job is a command that is run in the background, allowing you to continue running other commands while the job is executing.

To run a command as a background job, you can use the & symbol at the end of the command. For example:

command &

This will run the command in the background, and the shell will return the control to you immediately. You can then use the jobs command to see a list of the background jobs that are running or stopped in the current shell.

If a background job is stopped (e.g., by pressing CTRL+Z while it is running), you can use the bg command to resume it in the background. To do this, you can use the bg command followed by the job number. The job number is the number assigned to the job when it was started. You can use the jobs command to see a list of the background jobs, along with their job numbers.

For example, to resume the second stopped background job in the background, you can use the following command:

$ bg %2

This will resume the second stopped background job in the background, allowing you to continue running other commands while it is executing.

The bg command is often used in combination with the jobs and fg commands. The jobs command displays a list of the background jobs, and the fg command is used to bring a background job to the foreground.


Summary:

The top command is a Linux utility that displays real-time information about the running processes on a system. It provides a list of the top processes sorted by CPU usage, as well as other information such as memory usage, load average, and uptime. The list is updated in real-time and can be customized using various options, such as the ability to filter by a specific user, sort by a specific metric, or set the update interval. The df command in Linux is used to display the amount of disk space used and available on a file system. It provides a list of file systems on the system, along with information about the total size, used space, available space, and percentage of usage for each file system.

The jobs command in Linux is used to display a list of the background jobs that are running or stopped in the current shell. A background job is a command that is run in the background, allowing the user to continue running other commands while the job is executing. The jobs command displays a list of the background jobs, along with their job numbers and status. The fg command is used to bring a background job to the foreground, and the bg command is used to resume a stopped background job in the background. To run a command as a background job, the & symbol can be used at the end of the command. To bring a background job to the foreground, the fg command is used with the job number or the name of the command that was used to start the background job. To resume a stopped background job in the background, the bg command is used with the job number. The jobs, fg, and bg commands are often used together to manage and control the execution of background jobs in Linux.

Questions for review:

  1. What is the top command used for in Linux?
  2. How do you run the top command in Linux?
  3. What information does the top command display about running processes on a system?
  4. Can you filter the list of processes displayed by the top command by a specific user?
  5. Can you use the top command to sort the list of processes by a specific metric?
  6. What is the df command used for in Linux?
  7. How do you run the df command in Linux?
  8. What information does the df command display about file systems on a system?
  9. Can you display the sizes of file systems in “human-readable” format using the df command?
  10. Can the df command display the file system type, mount point, and device name for each file system?
  11. Can you use the df command to display the disk usage for a specific file system?
  12. What is the purpose of the jobs command in Linux?
  13. How do you run a command as a background job in Linux?
  14. How do you bring a background job to the foreground using the fg command in Linux?
  15. How do you resume a stopped background job in the background using the bg command in Linux?
  16. How can you view a list of background jobs and their job numbers in Linux?
  17. How can you use the fg and bg commands in combination with the jobs command in Linux?

License

Developers ultimate guide: Linux Bash scripting Copyright © 2022 by Matin Maleki. All Rights Reserved.

Share This Book