2017-06-23 151 views
1

我在我的shell腳本中定義了一個名爲pk的交互功能來殺死程序,如pk emacs殺死emacs程序,但是如果有多個實例在運行,那麼請你選擇pid殺死或殺死他們。如何在Linux終端上顯示GUI應用程序的窗口標題

這是偶爾出現,當我的Emacs的一個凍結,因爲我在我公司CentOS是老了,但在我的腳本功能pk,我用ps過濾命令和它們的PID,據我所知ps告訴任何窗口標題在這種情況下,它只是打印一個或多個「/ usr/bin/emacs」,沒有更多的細節,我不知道哪個PID會凍結或無響應,我將殺死它。

我知道我可以使用系統工具,如System Activity(KDE)檢查窗口的標題和殺死程序,但我要殺死使用pk功能的終端程序,所以有喜歡ps但表現出任何工具「窗口 - 標題+命令+ PID「,所以我可以在我的腳本中使用,以殺死該程序。

因爲如果你用終端打開一個使用vim或emacs的文件,ps帶有選項會顯示它正在編輯的文件,所以我知道PID的細節並知道哪一個要殺死,所以這裏的Window標題是如System Activity中的Window標題。

當然,如果獲得寡婦頭銜是錯誤的方式,如果有人知道如何殺死同一個程序的多個實例之一,就像我說的,答案是值得歡迎的。

回答

1

我剛剛發現了另一個解決方案,我可以在我的pk功能使用殺下面的行凍結的emacs:

kill -SIGUSR2 (xprop | grep -i pid | grep -Po "[0-9]+") 

(xprop...)部分將返回PID,當你在一個GUI程序點擊使用鼠標。

如果有人在我的pk功能有趣,這裏是(請注意,我用魚貝,所以這是魚的腳本功能):

function pk --description 'kill processes containg a pattern' 
    set done 1 
    set result (psg $argv[1] | wc -l) 
    if test $result = 0 
     echo "No '$argv[1]' process is running!" 
    else if test $result = 1 
     psg $argv[1] | awk '{print $2}' | xargs kill -9 
     if test $status = 123 # Operation not permitted 
      read -p 'echo "Use sudo to kill it? [y/N]: "' -l arg 
      if test "$arg" = "y" 
       psg $argv[1] | awk '{print $2}' | xargs sudo kill -9 
      end 
     end 
    else 
     psg $argv[1] 
     while test $done = 1 
      read -p 'echo "Kill all of them or specific PID? [y/N/pid]: "' -l arg 
      if test "$arg" = "y" 
       psg $argv[1] | awk '{print $2}' | xargs kill -9 
       if test $status -eq 123 # Operation not permitted 
        read -p 'echo "Use sudo to kill them all? [y/N]: "' -l arg2 
        if test "$arg2" = "y" 
         psg $argv[1] | awk '{print $2}' | xargs sudo kill -9 
        end 
       end 
       set done 0 
      else if test $arg -a "$arg" != "y" -a "$arg" != "n" 
       # the fist cond in test means you typed something, RET will not pass 
       if test (psg $argv[1] | awk '{print $2}' | grep -i $arg) 
        kill -9 $arg #2>/dev/null 
        if test $status -eq 1 # kill failed 
         read -p 'echo "Use sudo to kill it? [y/N]: "' -l arg2 
         if test "$arg2" = "y" 
          sudo kill -9 $arg 
         end 
        end 
        echo -e "Continue...\n" 
        usleep 100000 
        psg $argv[1] 
       else if test "$arg" = "p" 
        # This may be used for frozen emacs specifically, -usr2 or -SIGUSR2 
        # will turn on `toggle-debug-on-quit`, turn it off once emacs is alive again 
        # Test on next frozen Emacs 
        kill -SIGUSR2 (xprop | grep -i pid | grep -Po "[0-9]+") 
        # kill -usr2 (xprop | grep -i pid | grep -Po "[0-9]+") 
        return 
       else 
        echo "PID '$arg[1]' is not in the list!" 
        echo 
       end 
       set done 1 
      else 
       # RET goes here, means `quit` like C-c 
       set done 0 
      end 
     end 
    end 
end 
相關問題