Linux Process Management

A process is an instance of a program that is currently running on your system. In the context of web servers, processes are responsible for handling incoming requests from clients and returning responses. If one or more processes fail to respond, it can result in downtime and affect customer experience. To manage processes in Linux, we use various commands such as `ps`, `kill`, `top`, etc. Let’s take a look at some of these commands and how they can be used for troubleshooting web server issues:

1. ps (process status) This command is used to list running processes on your system. It provides information about the process ID, CPU usage, memory usage, and other details. To see all running processes, use `ps aux`. If you want to filter by a specific user or process name, add appropriate options such as `-u` for user or `-C` for command:

bash
# This script uses the `ps` command to list running processes on the system.
# The `aux` option displays all processes, including those from other users and daemons.
# The output is then piped to the `grep` command to filter for processes containing "apache".
# This will show all processes related to the Apache web server.

# The `ps` command will display the following information for each process:
# - USER: the user who started the process
# - PID: the process ID
# - %CPU: the percentage of CPU usage by the process
# - %MEM: the percentage of memory usage by the process
# - COMMAND: the command used to start the process

# The `grep` command will filter the output of `ps` to only show processes containing "apache".
# This will include both the root and www-data users, as shown in the original script.



#!/bin/bash
ps aux | grep apache
# The `ps` command is used to list all running processes, and the output is piped to the `grep` command.
# The `grep` command filters the output to only show processes containing "apache".

# The output of the script will be similar to the following:
# root      1234  0.5 16.7 89280 10440 ? Ssl   11:05 0:00 /usr/sbin/apache2 -k start
# www-data  1235  0.0 16.6 89280 10440 ? S    11:05 0:00 /usr/sbin/apache2 -k start

# The first column shows the user who started the process, followed by the process ID, CPU and memory usage, and the command used to start the process.
# In this case, both processes are related to the Apache web server, as indicated by the command "/usr/sbin/apache2 -k start".

In this example, we used `grep` to filter the output and show only processes related to Apache (`ps aux | grep apache`). The output shows us that there are two instances of Apache running with PIDs 1234 and 1235.

2. kill This command is used to terminate a process by sending it a signal. To stop a specific process, use `kill `. If you want to send a different signal (such as SIGTERM or SIGKILL), add the appropriate option:

# This script is used to terminate a specific process by sending it a signal.
# The process to be terminated is identified by its PID (Process ID).
# To stop a specific process, use `kill <PID>`.
# If you want to send a different signal (such as SIGTERM or SIGKILL), add the appropriate option.

# The following command kills the process with PID 1234.
# However, it is recommended to use the `kill` command with the `-s` option to specify the signal to be sent.
# For example, `kill -s SIGTERM 1234` will send the SIGTERM signal to the process with PID 1234.

kill -s SIGTERM 1234

In this example, we used `kill` to terminate Apache process with PID 1234. If you want to send a different signal (such as SIGTERM or SIGKILL), add the appropriate option:

bash
# This script uses the `kill` command to terminate a process with a specific PID.
# The user can specify which signal to send to the process by adding the appropriate option.

# The `kill` command is used to send signals to processes. 
# In this case, the `-SIGTERM` option is used to send the SIGTERM signal to the process.
# This signal is used to gracefully terminate a process.

# The PID of the process to be terminated is specified after the signal option.
# In this example, the PID is 1235.


bash
$ kill -SIGTERM 1235


# To terminate a process with a different signal, simply replace `-SIGTERM` with the desired signal option.
# For example, to send the SIGKILL signal, the script would be:
bash
$ kill -SIGKILL 1235


# It is important to note that the process will only be terminated if the user has the appropriate permissions to do so.
# If the user does not have the necessary permissions, the process will not be terminated and an error message will be displayed.

In this example, we used `kill` with the `-SIGTERM` option to send a termination signal to Apache process with PID 1235.

3. top This command is used to monitor system resources and processes in real time. It provides information about CPU usage, memory usage, and other details for all running processes:

bash
#!/bin/bash # This line specifies the interpreter to be used for executing the script.

# This script uses the `top` command to monitor system resources and processes in real time.

top # This command is used to display information about system processes and resources.


# 1. Added a shebang line to specify the interpreter.
# 2. Added a comment to explain the purpose of the script.
# 3. Added a comment to explain the purpose of the `top` command.
# 4. Removed the `$` symbol, as it is not necessary for executing the `top` command.
# 5. Added a blank line for better readability.

In this example, we used `top` to display the current resource utilization on our system. The output shows us a list of running processes sorted by CPU usage. To filter by a specific process or user, use appropriate options such as `-u` for user or `-C` for command:

# This script uses the `top` command to display the current resource utilization on the system.
# The output shows a list of running processes sorted by CPU usage.
# To filter by a specific user, the `-u` option is used followed by the username.
# To filter by a specific command, the `-C` option is used followed by the command name.
# The `grep` command is used to search for the specified keyword in the output of `top`.
# In this case, we are searching for processes owned by the `www-data` user that contain the word `apache`.


#!/bin/bash
top -u www-data | grep apache

In this example, we used `top` with the `-u` option to filter by a specific user (www-data) and show only processes related to Apache. The output shows us a list of running Apache processes sorted by CPU usage for that particular user. By using these commands in combination, you can troubleshoot web server issues such as high resource utilization or process failures. For instance, if you notice that your Apache processes are consuming too much memory, you can use `top` to identify the problematic process and then terminate it with `kill`. This will free up resources and prevent downtime for your customers.

SICORPS