Linux Pattern-Based Killing

LinuxLinuxBeginner
Practice Now

Introduction

Welcome to the high-flying world of the aerial city Stratos. Floating amongst the clouds, this marvel of engineering is a bustling hub of activity, home to renowned skyfarer Zephyr. As an elite cyberspace navigator, Zephyr's responsibility is to maintain the digital winds that allow information to flow seamlessly across the city's vast networks.

In a recent turn of events, a swarm of rogue processes has been detected, threatening the stability of Stratos's servers. The Council of Elders has summoned Zephyr for a critical mission: to strategically stop these errant processes from overwhelming the system. Harnessing the power of pattern-based process termination in Linux, Zephyr needs to use his expertise with pkill to restore order to the chaos.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("`Linux`")) -.-> linux/ProcessManagementandControlGroup(["`Process Management and Control`"]) linux/ProcessManagementandControlGroup -.-> linux/pkill("`Pattern-Based Killing`") subgraph Lab Skills linux/pkill -.-> lab-271359{{"`Linux Pattern-Based Killing`"}} end

Identify Rogue Processes

In this step, you'll assist Zephyr by identifying the processes that are threatening the system's harmony. Zephyr has determined that these processes are all running a certain application script known as rogue_app.sh. To simulate this environment, you will need to create multiple instances of this script and then use the pkill command to terminate them.

First, let's create the rogue_app.sh script:

#!/bin/bash
## Path: ~/project/rogue_app.sh
while true; do
  : ## The rogue process is stuck in an endless loop
done

Make the script executable and run multiple instances in the background:

chmod +x ~/project/rogue_app.sh
for i in {1..5}; do
  ~/project/rogue_app.sh &
done

Check your running processes using the ps command. Notice the PIDs (Process IDs) of your rogue processes:

ps aux | grep rogue_app.sh

Now, let's remove these processes with pkill. In this step, use the script's name as a pattern:

pkill -f rogue_app.sh

Confirm they have been terminated:

ps aux | grep rogue_app.sh

You should see no instances of rogue_app.sh running.

Practice selective termination

Zephyr has encountered another cluster of processes named service_worker.sh that have been compromised. However, not all service_worker instances are rogue; only those with a specific argument, --malfunctioning, need termination. Your task is to terminate only the processes with this argument without affecting the others.

Create the script service_worker.sh:

#!/bin/bash
## Path: ~/project/service_worker.sh
sleep 30

Make the script executable and launch multiple instances of the script with and without the --malfunctioning argument:

chmod +x ~/project/service_worker.sh
for i in {1..3}; do
  ~/project/service_worker.sh --malfunctioning &
  ~/project/service_worker.sh &
done

View results:

ps aux | grep service_worker.sh

To remove only the malfunctioning services, use pkill with a pattern that includes the argument:

pkill -f 'service_worker.sh --malfunctioning'

Make sure only the problematic instances have been killed:

ps aux | grep service_worker.sh

The process list should now only show service_worker.sh instances without the --malfunctioning argument.

Summary

In this lab, we embarked on an exciting journey to aid Zephyr, a skyfarer in the high-tech aerial city of Stratos, to terminate rogue processes using pkill. Starting by crafting a scenario with a direct pattern match in step one, we advanced to a more selective process termination in step two, targeting processes with a specific argument. This experience highlighted the importance of mastering pkill for destructive precision, valuable for managing systems and maintaining their stability. Your efforts in completing this lab have not only served the residents of Stratos but have also prepared you to orchestrate process control in your own Linux adventures.

Other Linux Tutorials you may like