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

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

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多個相關(guān)進程,如果單獨去一個一個kill,很不方便且容易出錯。通過定義一個函數(shù)psk可以實現(xiàn)查找并kill相關(guān)進程的功能。例如,示例中通過sleep命令模擬啟動了3個進程,啟動后可以查看到3個進程分別在后臺運行,執(zhí)行psk 1234命令即可同時kill這3個進程,執(zhí)行完psk 1234命令后,再次查詢相關(guān)進程,發(fā)現(xiàn)進程已經(jīng)不存在。

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個技巧,會發(fā)現(xiàn)psg查詢進程時會包含自身進程,psk在kill相關(guān)進程時會打印一條信息:kill: sending signal to 18073 failed: No such process,這是因為查詢進程時,沒有將自身進程排除導(dǎo)致。示例中第一次執(zhí)行ps -ef | grep 1234,發(fā)現(xiàn)有一個17902進程,這個進程就是執(zhí)行ps -ef | grep 1234中的grep命令。如果執(zhí)行ps -ef | grep [1]234會發(fā)現(xiàn),已經(jīng)將自身進程排除了。當(dāng)然,也可以通過grep的-v選項實現(xiàn)過濾自身的功能,如下:

$ 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

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

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

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容