📜  linux kill all process of a user - Shell-Bash (1)

📅  最后修改于: 2023-12-03 15:17:20.704000             🧑  作者: Mango

Linux Kill All Process of a User - Shell-Bash

Introduction

In Linux, there might be situations where you need to kill all processes of a specific user. This can be useful when troubleshooting or when you want to free up system resources. In this guide, we will explore different ways to kill all processes of a user using Shell-Bash scripting.

Terminate all Process of a User using pkill Command:

The pkill command is used to send signals to processes based on their names or other attributes. To terminate all processes of a specific user, you can use the following command:

pkill -U username

Replace username with the actual username of the user whose processes you want to terminate. The -U option specifies the user whose processes should be terminated.

Terminate all Process of a User using killall Command:

The killall command is used to terminate processes based on their names. To kill all processes of a specific user, you can use the following command:

killall -u username

Replace username with the actual username of the user whose processes you want to kill. The -u option specifies the user whose processes should be terminated.

Terminate all Process of a User using a Shell Script:

You can also write a shell script to automate the process of killing all processes of a specific user. Here's an example shell script:

#!/bin/bash

# Get the user to kill processes for
read -p "Enter username: " username

# Get the list of process IDs for the user
pids=$(ps -u $username -o pid=)

# Kill each process
for pid in $pids
do
  kill $pid
done

Save the above script in a file, for example, kill_user_processes.sh. Make the script executable using the following command:

chmod +x kill_user_processes.sh

To execute the script, run the following command:

./kill_user_processes.sh

The script will prompt you to enter the username for which you want to kill processes. It will then obtain the list of process IDs for that user and kill each process individually.

Conclusion

In this guide, we explored different ways to kill all processes of a user in Linux using Shell-Bash scripting. You can choose the method that suits your requirements and automate the process if needed. Remember to use these commands and scripts with caution, as killing processes can have consequences on the system's stability and the user's work.