一個UNIX平台上kill進程的Shell程式,killprog

來源:互聯網
上載者:User

來源程式來自 http://www.unixreview.com/documents/urm0108e/

原來程式中過濾掉自身進程的那個語句有bug,如果把killprog放到一個$PATH變數的路徑中,然後從其他路徑啟動killprog時,$0是帶有路徑的,後來我把$0改為${0##*/},就可以過濾掉了

後來我還給這個作者寫了個email,想探討一下,別人也沒有理我,難道是他的英語水平太差,呵呵

 

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

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.