A shell program for kill processes on a UNIX platform, killprog

Source: Internet
Author: User

Source program from http://www.unixreview.com/documents/urm0108e/

The statement in the original program that filters out its own process has a bug. If you put killprog in the path of a $ PATH variable and then start killprog from other paths, $0 carries a path, later, I changed $0 to ${0 # */} to filter it out.

Later, I wrote an email to the author. I want to discuss whether others have ignored me. Is it because his English skills are too poor?

 

Run the following command in the Korn shell:

#! /Bin/KSh

# Killprog: Kill A program by name instead of by process ID.

# If there is more than one copy, display a list, and

# Ask to pick the right one.

# Author: Fred Brunet

# Modify: gehongbin 2001.8.30 $0 =>$ {0 ##*/}

Kill =/tmp/killprogout. $

If [$ #-EQ 0]

Then

Printf "Usage: killprog name \ n"

Exit 1

Fi

# Eliminate present program and 'grep $ 1' from process

PS-Ef | grep-V "grep $1" | grep-V ${0 ##*/} | grep $1> $ kill

Count = 'wc-L $ kill | awk '{print $1 }''

If [$ count-ge 1]

Then

Awk '{printf "% 2D: % s \ n", NR, $0}' <$ kill

Else

Printf "no matches to kill \ n"

If [-F $ kill]

Then

Rm $ kill

Fi

Exit 1

Fi

Printf "which of the above do you mean: Enter number or '0' to quit :"

While true

Do

Read lineno

# Non-numeric data returns 0

Lcnt = 'echo $ lineno | awk '{if ($0 ~ /[^ 0-9]/)

Print 0

Else

Print 1 }''

If [$ lcnt-EQ 0]

Then

Printf "valid input: 1-$ count or '0' to quit. reenter number :"

Continue

Fi

# Default to 0 and quit

Lineno =$ {lineno: = 0}

If [$ lineno-EQ 0]

Then

If [-F $ kill]

Then

Rm $ kill

Fi

Exit 1

Fi

If [$ lineno-GT $ count-o $ lineno-lt 0]

Then

Printf "valid input: 1-$ count or '0' to quit. reenter number :"

Else

Break

Fi

Done

Flist = 'sed-n "$ {lineno} p" $ kill'

Echo $ flist | awk '{printf "% s \ n", $0 }'

Killproc = 'echo $ flist | awk '{print $2 }''

Printf "Kill ($ killproc )? Y/N"

Read answer

Case $ answer in

Y | Y) Kill $ killproc

Printf "$ killproc terminated! \ N ";;

*)

Printf "Nothing killed \ n ";;

Esac

If [-F $ kill]

Then

Rm $ kill

Fi

# End killprog

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.