preload
十月 07

發現有些 Linux 上有個 pidof 的指令可以取得執行中程序的pid。

OSX 並沒有內建這個指令,雖然我知道 ps aux | grep XXX 也可以做到相同功能,畢竟顯示的結果不夠乾淨。

試著去找 pidof 的 source code 來編譯,但我編不起來XD

幸好有其他高手提供了 scrpit

在此節錄程式碼以資參考:
Shell Script:

1
2
#!/bin/sh
ps axc|awk "{if (\$5==\"$1\") print \$1}";

Perl:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#!/usr/bin/perl
 
@procs = `ps -x`;
 
for $proc (@procs ) {
    if( $proc =~ /\s+(\d+)\s+\S+\s+\S+\s+\S+\s+(\S+)/ ) {
	$pid = $1;
	$name = $2;
	if( $name =~ /$ARGV[0]/ ) {
	    print "$pid ";
	}
    }
}
 
print "\n";

link:macosxhints.com – Create a pidof command to find PID numbers easily

溫故知新

載入中…

相關文章:

Tagged with:

Leave a Reply