Common bash script for stopping Processes

Source: Internet
Author: User

650) this. width = 650; "border =" 0 "src =" http://www.bkjia.com/uploads/allimg/131228/1644131507-0.gif "style =" padding: 0px; margin: 0px; vertical-align: top; border: none; "alt =" "/> process_stopper.zip

 

#! /Bin/sh

# Http://code.google.com/p/mooon

# Stop a process with a specified name

# Features:

#1. Only processes under the current user name will be stopped

#2. You can specify the command line parameters and execute the command to stop accurately.

 

# Check Parameters

# Parameter 1: name of the process to be stopped or its complete command line or part of command line

If test $ #-ne 1; then

Printf "\ 033 [1; 33 musage: $0 process_cmdline \ 033 [m \ n"

Exit 1

Fi

 

Process_cmdline = $1 # process name or process running command line

Cur_user = 'whoam' # current user

Uid = 'id-u $ cur_user '# user id

 

# Obtain the process name

Process_name = $ (basename 'echo "$ process_cmdline" | cut-d ""-f1 ')

# Obtain the process ID list

Pid_set = 'ps-C "$ process_name" h-o euser, pid, args | awk '{if ($1 = uid | $1 = cur_user) & match ($0, process_cmdline) printf ("% s \ n", $2);} 'uid = $ uid cur_user = $ cur_user process_cmdline = "$ process_cmdline "'

 

# Kill all processes cyclically

For pid in $ pid_set

Do

# Echo $ pid; exit; # Test

Kill $ pid # Start SIGTERM Signal

Sleep 2 # Send a SIGKILL signal after 2 seconds

Kill-9 $ pid

Done

 

 

This article is from the "Fei Yue" blog, please be sure to keep this source http://mooon.blog.51cto.com/1246491/961289

Related Article

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.