Processes are essentially programs that are currently running on your system. They can be anything from a web browser to a background task like updating software or syncing files. And managing them is crucial if you want to keep your machine humming along smoothly.
So how do we manage these processes in Linux? Well, let’s start with the basics checking which processes are currently running on our system. For that, we can use the trusty old `ps` command (short for “process status”). This will give us a list of all the active processes and some basic information about them like their PID (Process ID), CPU usage, memory usage, etc.
Here’s an example:
#!/bin/bash
# This is a bash script that will list all active processes on the system using the `ps` command.
# The `ps` command with the `aux` option will give us a list of all active processes and their basic information.
# The output will be piped to the `head` command to limit the number of lines to 10.
ps aux | head -n 10
# The first line of the output will show the column headers for the information provided.
# These include the user, process ID, CPU usage, memory usage, virtual memory size, resident set size, terminal, status, start time, and command.
# The following lines will show the information for each active process.
# This includes the user who started the process, the process ID, CPU and memory usage, virtual memory size, resident set size, terminal, status, start time, and command.
# The `USER` column shows the user who started the process.
# The `PID` column shows the process ID.
# The `%CPU` column shows the CPU usage for the process.
# The `%MEM` column shows the memory usage for the process.
# The `VSZ` column shows the virtual memory size for the process.
# The `RSS` column shows the resident set size for the process.
# The `TTY` column shows the terminal associated with the process.
# The `STAT` column shows the status of the process.
# The `START` column shows the start time of the process.
# The `TIME` column shows the total CPU time used by the process.
# The `COMMAND` column shows the command used to start the process.
As you can see, this command shows us a list of the top ten processes running on our system. The `ps aux` part tells it to show all processes (including those owned by other users), and the `head -n 10` part limits the output to just the first 10 lines.
Now that we know how to check which processes are currently running, managing them. One of the most common tasks is killing a process when it’s misbehaving or using too many resources. To do this, you can use the `kill` command followed by the PID (Process ID) of the process you want to kill:
#!/bin/bash #shebang to specify the interpreter to use
# This script is used to kill a process by its PID
# Check if the user has provided a PID as an argument
if [ $# -eq 0 ]; then #check the number of arguments provided
echo "Please provide a PID as an argument." #print an error message
exit 1 #exit the script with an error code
fi
# Store the PID provided by the user in a variable
pid=$1
# Check if the PID is a valid number
if ! [[ "$pid" =~ ^[0-9]+$ ]]; then #check if the PID is a number
echo "Please provide a valid PID." #print an error message
exit 1 #exit the script with an error code
fi
# Check if the process with the given PID is currently running
if ! ps -p $pid > /dev/null; then #check if the process exists
echo "Process with PID $pid is not currently running." #print an error message
exit 1 #exit the script with an error code
fi
# Kill the process with the given PID
kill $pid #send a signal to the process to terminate it
# Check if the process was successfully killed
if [ $? -eq 0 ]; then #check the exit status of the previous command
echo "Process with PID $pid has been successfully killed." #print a success message
else
echo "Failed to kill process with PID $pid." #print an error message
fi
exit 0 #exit the script with a success code
This will send a signal to the process with PID 12345 telling it to terminate. If that doesn’t work, you can try sending a more forceful signal using `kill -9
Another common task when managing processes is restarting or stopping them. To do this, you can use the `service` command followed by the name of the service you want to manage:
bash
# This script uses the `systemctl` command to stop the nginx service.
# The `sudo` command is used to run the script with root privileges.
# This is necessary as only root users have the permission to manage services.
# The `systemctl` command is used to manage system services.
# The `stop` option is used to stop a service.
# The `nginx` argument specifies the name of the service to be stopped.
# The `sudo` command is followed by the `systemctl` command and its options and arguments.
# This ensures that the command is executed with root privileges.
# The `systemctl` command sends a SIGTERM (signal 15) to the nginx service.
# This signal requests the service to gracefully stop and clean up any resources it is using.
# If the service does not respond to the SIGTERM signal within a certain time frame,
# the `systemctl` command will send a SIGKILL (signal 9) to the service.
# This signal forcefully terminates the service and may result in data loss or corruption.
# By stopping the nginx service, any active connections to the web server will be closed.
# This may cause temporary disruption to any websites or applications hosted by nginx.
# It is important to properly manage processes to ensure the stability and security of a system.
# Restarting or stopping services can help resolve issues or apply updates without having to reboot the entire system.
bash
# This script uses the `systemctl` command to stop the nginx service.
sudo systemctl stop nginx
This will stop the NGINX web server from running on your machine. You can also start it again using `sudo systemctl start nginx`. And if you want to check its status, use `sudo systemctl status nginx`.
Finally, some more advanced process management techniques like creating and managing background processes (also known as daemons). To do this, we can use the `nohup` command followed by our desired script or program:
#!/bin/bash
# This is a bash script for managing nginx and creating background processes.
# To start nginx, use the `sudo systemctl start nginx` command.
sudo systemctl start nginx
# To stop nginx, use the `sudo systemctl stop nginx` command.
sudo systemctl stop nginx
# To restart nginx, use the `sudo systemctl restart nginx` command.
sudo systemctl restart nginx
# To check the status of nginx, use the `sudo systemctl status nginx` command.
sudo systemctl status nginx
# To create and manage background processes, we can use the `nohup` command followed by our desired script or program.
# The `nohup` command allows the process to continue running even after the terminal is closed.
nohup myscript.sh &
# To view the list of background processes, we can use the `jobs` command.
jobs
# The output of `jobs` shows the process ID (PID) and status of each background process.
# The `[1]` indicates the job number, and `23456` is the PID.
# The `Running` status indicates that the process is currently running in the background.
This will run `myscript.sh` in the background and ignore any signals that would normally terminate it (like SIGINT or SIGTERM). The `jobs` command shows us a list of our currently running jobs, including their PIDs and statuses.
And there you have it a comprehensive guide to managing processes in Linux! Remember, always be careful when working with system commands like these, as they can have serious consequences if used improperly.