Swiftorial Logo
Home
Swift Lessons
Tutorials
Learn More
Career
Resources

Using ps and kill - System Monitoring

Introduction

The ps and kill commands are essential tools for system monitoring and process management in Unix-like operating systems. The ps command is used to display information about active processes, while the kill command is used to terminate processes.

Using the ps Command

The ps command provides a snapshot of the current processes along with detailed information such as process ID (PID), terminal associated with the process (TTY), CPU usage, memory usage, and more.

Example:

To display all running processes, use:

ps aux
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1 0.0 0.1 22560 3648 ? Ss 10:00 0:01 /sbin/init
root 100 0.1 0.4 80000 16000 ? Ssl 10:00 0:05 /usr/lib/systemd/systemd-journald
user 1234 0.0 0.2 30000 8000 pts/0 Ss 10:05 0:00 -bash

The output columns are:

  • USER: The user who owns the process.
  • PID: The Process ID of the process.
  • %CPU: The CPU usage of the process.
  • %MEM: The memory usage of the process.
  • VSZ: The virtual memory size of the process.
  • RSS: The resident set size (physical memory) of the process.
  • TTY: The terminal associated with the process.
  • STAT: The process state.
  • START: The start time of the process.
  • TIME: The total CPU time used by the process.
  • COMMAND: The command that started the process.

Using the kill Command

The kill command is used to send a signal to a process, usually to terminate the process. The most common signal used is SIGTERM (signal 15), which requests a graceful shutdown of the process. For forcing a process to terminate, the SIGKILL (signal 9) is used.

Example:

To terminate a process with PID 1234, use:

kill 1234
Example:

To forcefully terminate a process with PID 1234, use:

kill -9 1234

Combining ps and kill

Often, you will need to use the ps command to find the PID of a process before using the kill command to terminate it. This can be done by using ps to list the processes and then grep to filter the output.

Example:

To find the PID of a process named "example_process" and terminate it:

ps aux | grep example_process
user 1234 0.0 0.2 30000 8000 pts/0 Ss 10:05 0:00 example_process

Then terminate the process with PID 1234:

kill 1234

Conclusion

Understanding and using the ps and kill commands are crucial for effective system monitoring and process management. The ps command helps you identify and monitor running processes, while the kill command allows you to manage those processes by sending signals to them.