Finding process count in Linux via command line

2019-01-17 15:38发布

I was looking for the best way to find the number of running processes with the 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.

9条回答
We Are One
2楼-- · 2019-01-17 16:13
result=`ps -Al | grep command-name | wc -l`
echo $result
查看更多
Viruses.
3楼-- · 2019-01-17 16:16
ps -Al | grep -c bash
查看更多
甜甜的少女心
4楼-- · 2019-01-17 16:24

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.

查看更多
登录 后发表回答