How to use shell scripts to kick users out of the system

Source: Internet
Author: User

The following is a script to kick the user out of the system:

#! /Bin/bash
Read-p "input your username" user
Ps aux | grep "^ $ user" | awk '{print $2}'>/opt/killuser // find a user's process and extract the process number
DAT = 'cat/opt/killuser'
For I in $ DAT // assign the process to the variable DAY
Do
Kill-9 $ I &>/dev/null // kill the pid corresponding to the process ID
Done
Rm-rf/opt/killuser

Explain the meaning of the script:

$ User: this variable is easy to understand and assign values to the keyboard. If you don't understand it, you can find the information.

For example, if the user currently logged on is lee, print the pid of the lee user, and extract the keyword awk to obtain the pid.

[Lee @ redhat root] $ ps aux | grep lee | awk '{print $2 }'
1878
1879
1900
1901
1902
[Lee @ redhat root] $ echo $ USER
Lee

Ps aux | grep "^ $ user" | awk '{print $2}'>/opt/killuser exports the extracted pid to a file.

 

Use kill-9 to force the kill.

 

 

 

 

 

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.