Grep по всем именам файлов в каталоге

311
Fueled By Coffee

Я хочу получить статус процесса для всех служб, работающих в данном каталоге. Прямо сейчас я могу проверить один за другим ps -ef | grep ServiceName. Но есть ли путь к lsкаталогу и psкаждому сервису?

Что-то вроде ps -ef | grep < ls?

1
вы ищете `ps -ef | grep "$ (ls)" `? incBrain 7 лет назад 1
@incBrain Точно, спасибо! Выкинь ответ, если хочешь кредит Fueled By Coffee 7 лет назад 0

3 ответа на вопрос

2
incBrain

Already posted it as a comment, this should do what you want:

ps -ef | grep "$(ls)" 
2
Kamil Maciorowski

In Bash:

ps -ef | grep "$(ls)" 

(The same line appeared in incBrain's comment while I was composing and testing the rest of my answer in Debian/Bash.)

It will generate garbage if the pattern appears in ps output as a command line argument (not the command) or as a part of the command. To reduce this I would use find instead of ls to obtain full path:

ps -ef | grep -f <(find -L "$(pwd)" -maxdepth 1 -type f) 

Still there may be some unwanted extra output.

Note that these are not

services running in a given directory

rather processes with executables in a given directory.

To tell current working directory of a process you may read /proc/<PID>/cwd. Next example is (quick and dirty) ps alternative and it shows that you can extract information from /proc in a form you want:

sudo bash -c 'for i in /proc/[1-9]* ; do PID=$(basename "$i"); E=$(readlink "$/exe"); D=$(readlink "$/cwd"); echo -e "PID=$\tEXE=$\tCWD=$" ; done | grep "CWD=$(pwd)$"' 

You need sudo to get access to all processes. If this isn't necessary and you want to run it as regular user, the part within ' delimiters is enough.

0
allo

Maybe something like this:

processes="$(ps -ef)" for file in *;do grep "$file" <<<"$processes" done