开发者

Finding process count in Linux via command line

开发者 https://www.devze.com 2023-01-03 14:05 出处:网络
I was looking for the best way to find the number of running processes with 开发者_如何学Gothe same name via the command line in Linux. For example if I wanted to find the number of bash processes run

I was looking for the best way to find the number of running processes with 开发者_如何学Gothe same name via the command line in Linux. For example if I wanted to find the number of bash processes running and get "5". Currently I have a script that does a 'pidof ' and then does a count on the tokenized string. This works fine but I was wondering if there was a better way that can be done entirely via the command line. Thanks in advance for your help.


On systems that have pgrep available, the -c option returns a count of the number of processes that match the given name

pgrep -c command_name

Note that this is a grep-style match, not an exact match, so e.g. pgrep sh will also match bash processes. If you want an exact match, also use the -x option.

If pgrep is not available, you can use ps and wc.

ps -C command_name --no-headers | wc -l

The -C option to ps takes command_name as an argument, and the program prints a table of information about processes whose executable name matches the given command name. This is an exact match, not grep-style. The --no-headers option suppresses the headers of the table, which are normally printed as the first line. With --no-headers, you get one line per process matched. Then wc -l counts and prints the number of lines in its input.


result=`ps -Al | grep command-name | wc -l`
echo $result


ps -Al | grep -c bash


You can try :

ps -ef | grep -cw [p]rocess_name

OR

ps aux | grep -cw [p]rocess_name

For e.g.,:

ps -ef | grep -cw [i]nit


Some of the above didn't work for me, but they helped me on my way to this.

ps aux | grep [j]ava -c

For newbies to Linux:

ps aux prints all the currently running processes, grep searches for all processes that match the word java, the [] brackets remove the process you just ran so it wont include that as a running process and finally the -c option stands for count.


List all process names, sort and count

ps --no-headers -A -o comm | sort | uniq -c

You also can list process attached to a tty

ps --no-headers a -o comm | sort | uniq -c

You may filter with:

ps --no-headers -A -o comm | awk '{ list[$1] ++ } END { for (i in list) { if (list[i] > 10) printf ("%20s: %s\n", i, list[i]) } }'


Following bash script can be run as a cron job and you can possibly get email if any process forks itself too much.

for i in `ps -A -o comm= --sort=+comm | uniq`; 
do 
    if (( `ps -C $i --no-headers | wc -l` > 10 )); then 
        echo `hostname` $i `ps -C $i --no-headers | wc -l` ;
    fi
done

Replace 10 with your number of concern.

TODO: "10" could be passed as command line parameter as well. Also, few system processes can be put into exception list.


You can use ps(will show snapshot of processes) with wc(will count number of words, wc -l option will count lines i.e. newline characters). Which is very easy and simple to remember.

ps -e | grep processName | wc -l

This simple command will print number of processes running on current server. If you want to find the number of process running on current server for current user then use -U option of ps.

ps -U root | grep processName | wc -l

change root with username.

But as mentioned in lot of other answers you can also use ps -e | grep -c process_name which is more elegant way.


ps aux | wc -l

This command shows number of processes running on the system by all the users.

For a specific user you can use the following command:

ps -u <username> | wc -l

replace with the actual username before running :)

0

精彩评论

暂无评论...
验证码 换一张
取 消