Let’s talk about the most exciting topic of all time process management on Linux!
If you’re like me, you probably spend hours staring at your terminal screen waiting for a command to finish running. But don’t freak out, bro, because I have some tricks up my sleeve that will make your life easier and more efficient.
Before anything else the basics. When you run a command in Linux, it creates a process. A process is essentially an instance of a program running on your system. You can see all currently running processes by using the ‘ps’ command:
#!/bin/bash
# This is a bash script that displays currently running processes using the 'ps' command.
# The 'ps' command is used to display information about processes running on the system.
# The 'aux' option is used to display all processes for all users.
# The '|' symbol is used to redirect the output of the 'ps' command to the 'head' command.
# The 'head' command is used to display the first 10 lines of the output.
# The output of the 'ps' command will be displayed in the following format:
# USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
# The first line of the output is the header, which displays the column names.
# The 'USER' column displays the username of the user who started the process.
# The 'PID' column displays the process ID of the process.
# The '%CPU' column displays the percentage of CPU usage by the process.
# The '%MEM' column displays the percentage of memory usage by the process.
# The 'VSZ' column displays the virtual memory size of the process.
# The 'RSS' column displays the resident set size of the process.
# The 'TTY' column displays the controlling terminal of the process.
# The 'STAT' column displays the status of the process.
# The 'START' column displays the start time of the process.
# The 'TIME' column displays the total CPU time used by the process.
# The 'COMMAND' column displays the command that started the process.
# The 'root' user is the owner of the first process with a process ID of 1, which is the 'init' process.
# The 'daemon' user is the owner of the next two processes with process IDs of 144 and 156.
# The '[ksoftirqd/0]' and '[kworker/0:0H-events_power_efficient]' are kernel threads, which are processes that run in the background and perform system tasks.
# The 'Ss' and 'S' in the 'STAT' column indicate that the processes are in the 'sleep' state.
# The 'Mar17' in the 'START' column indicates the date the process was started.
# The '0:00' in the 'TIME' column indicates the total CPU time used by the process.
# The '/sbin/init' and '[ksoftirqd/0]' processes are system processes that are essential for the functioning of the system.
# The '[kworker/0:0H-events_power_efficient]' process is a hardware event handler that is responsible for managing power-efficient events.
# The '384672' and '11080' in the 'VSZ' column indicate the virtual memory size of the processes in kilobytes.
# The '25920' and '3120' in the 'RSS' column indicate the resident set size of the processes in kilobytes.
# The '0.0' in the '%CPU' and '%MEM' columns indicate that the processes are currently not using any CPU or memory.
# The '?' in the 'TTY' column indicates that the processes are not associated with any terminal.
# The 'Ss' and 'S' in the 'STAT' column indicate that the processes are in the 'sleep' state.
# The 'Mar17' in the 'START' column indicates the date the processes were started.
# The '0:00' in the 'TIME' column indicates the total CPU time used by the processes.
# The '[ksoftirqd/0]' and '[kworker/0:0H-events_power_efficient]' processes are kernel threads, which are processes that run in the background and perform system tasks.
# The 'S' in the 'STAT' column indicates that the processes are in the 'sleep' state.
# The 'Mar17' in the 'START' column indicates the date the processes were started.
# The '0:00' in the 'TIME' column indicates the total CPU time used by the processes.
# The '[kworker/0:0H-events_power_efficient]' process is a hardware event handler that is responsible for managing power-efficient events.
This command shows you a list of all currently running processes, along with some useful information like CPU and memory usage. But what if you want to see only your own processes? Easy! Just add the ‘u’ flag to the ‘ps’ command:
# This command shows a list of all currently running processes, along with some useful information like CPU and memory usage.
# But what if you want to see only your own processes? Easy! Just add the 'u' flag to the 'ps' command:
# The 'ps' command is used to display information about processes.
# The 'aux' flag displays all processes on the system, regardless of the user.
# The '|' symbol is used to redirect the output of the 'ps' command to the 'grep' command.
# The 'grep' command is used to search for a specific pattern in the output.
# The '$USER' variable contains the username of the current user.
# By using the '$USER' variable in the 'grep' command, we are filtering the output of 'ps' to only show processes belonging to the current user.
# However, this script has a potential issue as the '$USER' variable may not be set or may contain special characters that could affect the 'grep' command.
# To fix this, we can enclose the '$USER' variable in double quotes to prevent any special characters from affecting the command.
# We can also use the '-e' flag in the 'grep' command to ensure that the pattern is matched exactly.
# Lastly, we can use the '-f' flag in the 'grep' command to search for the pattern in the full command line of the processes, rather than just the process name.
# This ensures that we only get processes belonging to the current user, without any potential issues.
ps aux | grep -e "$USER" -f -
This will show you all currently running processes that belong to your user account. Now, let’s say you have a long-running process that you want to monitor or stop later on. You can use the ‘bg’ command to run it in the background:
#!/bin/bash
# This line specifies the interpreter to be used for executing the script.
sleep 1000 & # This command creates a long-running process and runs it in the background.
# The '&' symbol at the end of the command tells the shell to run the process in the background.
# The '[1] 23456' output shows the job number and process ID of the background process.
This will start the ‘sleep’ command with a delay of 1,000 seconds and return you back to your terminal prompt. The ‘[1] 23456’ part is just an identifier for this process it tells you that job number 1 (the first background job) has been assigned PID 23456.
If you want to stop a running process, you can use the ‘kill’ command:
bash
# This script is used to stop a running process by using the 'kill' command.
# The '<PID>' placeholder should be replaced with the actual process ID of the process you want to stop.
# To find the process ID, you can use the 'ps' command to list all running processes and their IDs.
# For example, 'ps aux' will list all processes and their IDs in a table format.
# Once you have the process ID, you can use it in the 'kill' command to stop the process.
# The 'kill' command sends a signal to the process, telling it to terminate.
bash
$ ps aux # List all running processes and their IDs
$ kill 23456 # Replace 23456 with the actual process ID you want to stop
# After running the 'kill' command, the process will be terminated and you will be returned to your terminal prompt.
# The '[1] 23456' part is just an identifier for this process, it tells you that job number 1 (the first background job) has been assigned PID 23456.
Replace ‘
bash
# This script is used to find the PID of a process and terminate it.
# Replace '<process_name>' with the actual name of the process you want to terminate.
# Use the 'ps' command to list all running processes and pipe the output to 'grep' to filter for the specified process name.
# The 'aux' option displays all processes for all users.
# The output will include the PID of the process.
$ ps aux | grep <process_name>
# Once you have identified the PID, replace '<PID>' with the actual PID of the process you want to terminate.
# The 'kill' command is used to send a signal to the specified process, in this case the 'SIGTERM' signal to gracefully terminate the process.
# If the process does not terminate, you can use the 'kill -9' command to forcefully terminate it.
$ kill <PID>
This will show you all currently running processes with a name matching ‘
But what if you don’t know the exact PID? No problem! You can use the ‘pgrep’ command instead:
#!/bin/bash #shebang to specify the interpreter to be used
# This script searches for a process by name and kills it by using its PID.
# Prompt the user to enter the process name to be searched for.
read -p "Enter the process name to be searched for: " process_name
# Use 'pgrep' command to search for the process name and store the PID in a variable.
pid=$(pgrep "$process_name")
# Check if the PID is empty, which means the process was not found.
if [ -z "$pid" ]; then
echo "Process not found."
else
# If the process was found, prompt the user to confirm if they want to kill it.
read -p "Are you sure you want to kill the process with PID $pid? (y/n) " confirm
if [ "$confirm" == "y" ]; then
# If the user confirms, use 'kill' command to terminate the process.
kill "$pid"
echo "Process with PID $pid has been terminated."
else
echo "Process not terminated."
fi
fi
This will show you all currently running processes with a name matching ‘
And there you have it basic process management on Linux! Of course, there are many more advanced techniques and tools available, but these should be enough to get you started.