私はサーバーマネージャを作ろうとしていますが、いくつかのプロセスのプロセスIDとコマンドを取得する必要があります。
例えば:
ps ax | grep ./skulltag
4760 pts/2 Tl 0:02 ./skulltag-server
4793 pts/2 Tl 0:01 ./skulltag-server
4956 pts/2 Tl 0:01 ./skulltag-server -port 13000
4958 pts/2 Tl 0:26 ./skulltag-server -port 13001
どうすればプロセスを返すだけで、コマンド(./skulltag-server)またはその両方を返すことができますか?ありがとう。
回答:
回答№1の場合は3あなたはにパイプすることができます awk 出力するフィールドを選択する
例えば。 ps ax | grep ./skulltag | awk "{ print $1 }"
最初の列(pid)を出力します。
また、-oオプションを使用して ps その出力を変更する
回答№2の場合は3
あなたはawkを使うべきだと思います
ps ax | grep ./skulltag | awk "{print $1}" # Or $5, or $1 and $5
これにより、プロセスIDのリストが表示されます。
回答№3の場合は3
プロセスIDを取得するには、非標準でも便利です pgrep
.
ps ax | grep ./skulltag | awk "{ print $1 }"
おおよそ次のものと同等です:
pgrep skulltag
回答№4の場合は1
あなたは次の結果を解析することができます ps ax
を使用して awk
必要な列を抽出するには:
aix@aix:~/tmp$ ps ax | grep bash
1906 pts/5 Ss+ 0:00 bash
13749 pts/31 Ss 0:00 bash
27315 ? SN 0:00 /bin/bash /etc/cron.daily/backup
27648 pts/31 S+ 0:00 grep --color=auto bash
aix@aix:~/tmp$ ps ax | grep bash | awk "{print $1}"
1906
13749
27315
27652
aix@aix:~/tmp$ ps ax | grep bash | awk "{print $5}"
bash
bash
/bin/bash
grep