shell技巧分享(二)

這是一個系列文章,主要分享shell(部分功能僅適用於bash)的使用建議和技巧,每次分享3點,希望你能有所收穫。

1 ps + grep命令

$ sleep 1234 &
[1] 19340
$ sleep 1234 &
[2] 19342
$ sleep 1234 &
[3] 19344
$ alias | grep psg
alias psg='ps -ef | grep --color=auto'
$ ps -ef | grep 1234
root     19340  2159  0 14:22 pts/1    00:00:00 sleep 1234
root     19342  2159  0 14:22 pts/1    00:00:00 sleep 1234
root     19344  2159  0 14:22 pts/1    00:00:00 sleep 1234
root     19360  2159  0 14:23 pts/1    00:00:00 grep --color=auto 1234
$ psg 1234
root     19340  2159  0 14:22 pts/1    00:00:00 sleep 1234
root     19342  2159  0 14:22 pts/1    00:00:00 sleep 1234
root     19344  2159  0 14:22 pts/1    00:00:00 sleep 1234
root     19366  2159  0 14:23 pts/1    00:00:00 grep --color=auto --color=auto 1234

通過系統提供的alias命令將ps和grep命令合成一個命令psg,實現快速查找特定字符串的相關進程。比如執行ps -ef | grep 1234命令查找包含1234字符串的相關進程,通過執行alias psg='ps -ef | grep --color=auto'命令,定義一個新命令psg實現相同功能,更加方便快捷。

2 ps + kill命令

$ psk(){ ps -ef | grep "$1" |awk '{print $2}' | xargs kill -9;}
$ sleep 1234 &
[1] 18055
$ sleep 1234 &
[2] 18057
$ sleep 1234 &
[3] 18060
$ ps -ef | grep 1234
root     18055  2159  0 14:03 pts/1    00:00:00 sleep 1234
root     18057  2159  0 14:03 pts/1    00:00:00 sleep 1234
root     18060  2159  0 14:03 pts/1    00:00:00 sleep 1234
root     18067  2159  0 14:03 pts/1    00:00:00 grep --color=auto 1234
$ psk 1234
kill: sending signal to 18073 failed: No such process
[1]   Killed                  sleep 1234
[2]-  Killed                  sleep 1234
[3]+  Killed                  sleep 1234
$ ps -ef | grep 1234
root     18082  2159  0 14:03 pts/1    00:00:00 grep --color=auto 1234

在日常工作中,有時候需要kill多個相關進程,如果單獨去一個一個kill,很不方便且容易出錯。通過定義一個函數psk可以實現查找並kill相關進程的功能。例如,示例中通過sleep命令模擬啓動了3個進程,啓動後可以查看到3個進程分別在後臺運行,執行psk 1234命令即可同時kill這3個進程,執行完psk 1234命令後,再次查詢相關進程,發現進程已經不存在。

3 ps + grep 查找進程時忽略自身進程

$ sleep 1234 &
[1] 17888
$ sleep 1234 &
[2] 17889
$ sleep 1234 &
[3] 17890
$ ps -ef | grep 1234
root     17888  2159  0 14:01 pts/1    00:00:00 sleep 1234
root     17889  2159  0 14:01 pts/1    00:00:00 sleep 1234
root     17890  2159  0 14:01 pts/1    00:00:00 sleep 1234
root     17902  2159  0 14:01 pts/1    00:00:00 grep --color=auto 1234
$ ps -ef | grep [1]234
root     17888  2159  0 14:01 pts/1    00:00:00 sleep 1234
root     17889  2159  0 14:01 pts/1    00:00:00 sleep 1234
root     17890  2159  0 14:01 pts/1    00:00:00 sleep 1234

如果注意看前面2個技巧,會發現psg查詢進程時會包含自身進程,psk在kill相關進程時會打印一條信息:kill: sending signal to 18073 failed: No such process,這是因爲查詢進程時,沒有將自身進程排除導致。示例中第一次執行ps -ef | grep 1234,發現有一個17902進程,這個進程就是執行ps -ef | grep 1234中的grep命令。如果執行ps -ef | grep [1]234會發現,已經將自身進程排除了。當然,也可以通過grep的-v選項實現過濾自身的功能,如下:

$ ps -ef | grep 1234 | grep -v grep
root     17888  2159  0 14:01 pts/1    00:00:00 sleep 1234
root     17889  2159  0 14:01 pts/1    00:00:00 sleep 1234
root     17890  2159  0 14:01 pts/1    00:00:00 sleep 1234

現在,你可以在psg和psk命令的字符串第一個字符添加中括號[]試一下效果了。

注:將分享的alias或者函數寫入你的shell配置文件(如:~/.bashrc或/etc/profile)中,這樣每次打開終端都能使用。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章