Killer Bash Function: “Killing the use of kill -9”


What is kill -9 ?

The kill -9 , also known as SIGKILL, is a command used in Unix-like operating systems to forcefully terminate a process.

In the world of software development, dealing with processes and ports is a common task. Sometimes, we need to stop a process forcefully using kill -9, especially when it’s unresponsive. However, manually entering the process ID (PID) each time can be cumbersome. In this blog post, we’ll explore how to simplify this task by creating a bash function that automatically kills the last stopped port number.

Why do we need ?

When working on a project that involves multiple processes or servers, you may often find yourself needing to stop and restart services.
In some cases, a service may hang or become unresponsive, requiring you to use kill -9 to forcefully terminate it. Manually finding and entering the PID each time can be tedious, especially when you need to do it frequently.

Bash Magic !

To streamline this process, we can create a bash function that remembers the last stopped port number and automatically uses kill -9 on it.

Step 1: Open File

Step 2: Magical Method

I’ll Provide the code for you guys, you just copy and paste it in your terminal and I also added code explanation below

Step 3: Saving the changes

After entering the above code in your terminal, press ctrl+X and then press Y and the press enter/return key

Step 4: Test the changes
Now start your rails server and stop the server by pressing ctrl+z and now to kill the recently stopped port, press ctrl+k
Yay ! Congratulations you’ve killed the recently stopped port just using shortcut key

Note : If the above procedure didn’t works for you, please verify the code/steps properly or let me know by your comment

Keep Calm and Code On !
Leave A Comment