パラメータとして指定された特定のコマンドを実行しているユーザーの名前をすべて見つけたいと思います。grep を使用する必要があります。私は試しました: ps aux | grep $1 | -d" " -f1 を切り取りましたが、これは望ましい結果ではありません。
3 に答える
0
プロセスの情報を取得するためのトリックがありますが、プロセスを検索しているプロセスではなく、名前を正規表現にすることです。たとえば、 を検索している場合はls
、検索語を にしgrep '[l]s'
ます。grep
これは、それ自体または 1 文字のコマンド名を検索しない限り機能します。
これはprocname
私が使用するスクリプトです。ほとんどの POSIX シェルで動作します。
#! /bin/ksh
#
# @(#)$Id: procname.sh,v 1.3 2008/12/16 07:25:10 jleffler Exp $
#
# List processes with given name, avoiding the search program itself.
#
# If you ask it to list 'ps', it will list the ps used as part of this
# script; if you ask it to list 'grep', it will list the grep used as
# part of this process. There isn't a sensible way to avoid this. On
# the other hand, if you ask it to list httpd, it won't list the grep
# for httpd. Beware metacharacters in the first position of the
# process name.
case "$#" in
1)
x=$(expr "$1" : '\(.\).*')
y=$(expr "$1" : '.\(.*\)')
ps -ef | grep "[$x]$y"
;;
*)
echo "Usage: $0 process" 1>&2
exit 1
;;
esac
ではbash
、変数部分文字列操作を使用してexpr
コマンドを回避できます。
case "$#" in
1) ps -ef | grep "[${1:0:1}]${1:1}"
;;
*)
echo "Usage: $0 process" 1>&2
exit 1
;;
esac
これらは両方とも実行されps -ef
ます。必要に応じて使用できps aux
ます。「コマンド」名の検索は、コマンドのコマンド部分に限定されないため、procname root
ルートによって実行されるプロセスを見つけるために使用できます。また、一致は完全な単語に限定されません。そのために検討することができますgrep -w
(GNUgrep
拡張)。
これらの出力は、からのデータの完全な行ps
です。ユーザー (最初のフィールド) だけが必要な場合は、出力を にパイプしますawk '{print $1}' | sort -u
。
于 2013-03-30T19:03:41.373 に答える
0
/usr/ucb/ps aux | awk '/<your_command_as_parameter>/{print $1}'|sort -u
例:
> /usr/ucb/ps aux | awk '/rlogin/{print $1}' | sort -u
于 2013-03-28T13:33:10.457 に答える
0
私はあなたがこれを探していると思います。
# cat test.sh
ps aux | grep $1 | grep -v grep | awk '{print $1}'
# ./test.sh bash
root
root
root
于 2013-03-30T16:11:17.113 に答える