How to stop background process?

QuestionsQuestions8 SkillsProDec, 16 2025
0177

That's a very practical and important question, Labby! When you run a process in the background using &, you'll eventually need to stop it. Here's how you can do it:

The most common way to stop a background process is by using the kill command with the process's ID (PID). Here's a general approach:

  1. Find the Process ID (PID):
    You need to identify the unique number (PID) assigned to your background process. You can do this with the ps command, often filtered with grep. Since you started a Python HTTP server, you can look for Python processes:

    ps aux | grep http.server
    • ps aux: Lists all running processes with detailed information.
    • grep http.server: Filters the output to show only lines containing "http.server".

    You'll see output similar to this (the exact numbers will vary):

    labex       1456  0.0  0.1  11400  5888 pts/1    S    16:30   0:00 python3 -m http.server 8080
    labex       1460  0.0  0.0   6392  2320 pts/1    S+   16:30   0:00 grep --color=auto http.server

    In this example, the PID of your Python HTTP server is 1456. (Ignore the grep process itself, which is just the command you just ran).

  2. Kill the Process:
    Once you have the PID (1456 in our example), you can use the kill command:

    kill 1456

    If the process doesn't stop gracefully, you might need to use a more forceful kill:

    kill -9 1456

    The -9 option sends a SIGKILL signal, which forces the process to terminate immediately, without giving it a chance to clean up. Use this only if kill without -9 doesn't work, as it can sometimes lead to unclean shutdowns.

Alternative for the last background process:

If it's the very last command you put into the background in your current shell session, you might also see a job number like [1]. You can use the fg command to bring it to the foreground, and then use Ctrl+C to stop it:

  1. Bring to foreground:

    fg

    This will bring the python3 -m http.server process back to the foreground.

  2. Stop with Ctrl+C:
    Press Ctrl+C. This sends an interrupt signal, which usually gracefully stops the process.

For our current lab, you don't necessarily need to stop the server right away, as it's serving as your target for Nmap. However, knowing how to manage background processes is a fundamental skill in Linux!

Do you want to try finding and stopping the process now, or would you like to continue with the Nmap steps?

0 Comments

no data
Be the first to share your comment!