Check Running Processes


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Check Running Processes
# 1  
Old 05-31-2017
Wrench Check Running Processes

I want to check how many processes are running with same names and get their respective counts.

Code:
ps -ef|grep  -Eo 'process1|process2|process3| '|sort -u | awk '{print $2": "$1}'

Output would look like :

Code:
$ ps -ef|grep  -Eo 'process1|process2|process3| '|sort | uniq -c | awk '{print $2": "$1}'
: 108977
process1: 1
process2: 1
process3: 1

It should show 0 values against process1, process 2 and process 3 but it is calculating 1 process for grep command, how to ignore it and get 0.
# 2  
Old 05-31-2017
Does your system have pgrep? If so, does it support the -c, --count option?
Try this:
Code:
#!/bin/bash
for process in "$@"
do
   printf "%s: %d\n" $process $(pgrep -c $process)
done

Andrew
This User Gave Thanks to apmcd47 For This Post:
# 3  
Old 05-31-2017
Quote:
Originally Posted by apmcd47
Does your system have pgrep? If so, does it support the -c, --count option?
Try this:
Code:
#!/bin/bash
for process in "$@"
do
   printf "%s: %d\n" $process $(pgrep -c $process)
done

Andrew
Unfortunately my system doesn't support pgrep. Smilie
# 4  
Old 05-31-2017
Hi

Did you try | grep -v grep | ?

Code:
ps -ef|grep  -Eo 'process1|process2|process3| '|grep -v grep |sort | uniq -c | awk '{print $2": "$1}'


Last edited by rbatte1; 05-31-2017 at 06:00 AM.. Reason: Changed ICODE tags for CODE tags then wrapped ICODE tags round the in-line code snippet
This User Gave Thanks to radioactive9 For This Post:
# 5  
Old 05-31-2017
How about
Code:
ps -e -o comm | grep  -Eo 'process1|process2|process3| '|sort | uniq -c

When using ps always use the -o option to specify exactly which fields you need rather than use a full listing and remove the ones you don't.

Andrew
This User Gave Thanks to apmcd47 For This Post:
# 6  
Old 05-31-2017
With the -E flag that you are already using, the search is for a regular expression. You can include an expression so that grep will not find itself like this:-
Code:
ps -ef | grep -E "Hell[o]"

The [o] matches the single character o which seems illogical until you recognise that the grep process will therefore not match, so it excludes itself.

You have several options separated with pipes so you pick the row if you match any of the items so you will need to do this to each part.


Two more thoughts:-
  • You have a search pattern of a space. Is that really what you want? I would expect much more output if that were true.
  • Your sort -u seems in the wrong place. Perhaps this might be better:-
    Code:
    ps -eo comm= | grep -Eo "whateve[r]|somethin[g]|els[e]" | sort | uniq -c

    I use -o comm= to strip out the headings.

    You do end up with the output the other way round. Is that a problem?


I hope that this helps,
Robin
These 3 Users Gave Thanks to rbatte1 For This Post:
# 7  
Old 05-31-2017
Quote:
Originally Posted by radioactive9
Hi

Did you try | grep -v grep | ?

Code:
ps -ef|grep  -Eo 'process1|process2|process3| '|grep -v grep |sort | uniq -c | awk '{print $2": "$1}'

Hey Radioactive,
Am sorry, it would give same result as before.
Code:
$ ps -ef|grep  -Eo 'process1|process2|process3| '|grep -v grep |sort | uniq -c | awk '{print $2": "$1}'
: 108766
process1: 1
process2: 1
process3: 1

---------- Post updated at 05:04 AM ---------- Previous update was at 05:02 AM ----------

Quote:
Originally Posted by apmcd47
How about
Code:
ps -e -o comm | grep  -Eo 'process1|process2|process3| '|sort | uniq -c

When using ps always use the -o option to specify exactly which fields you need rather than use a full listing and remove the ones you don't.

Andrew
Thanks Andrew, it was helpful to certain extent. When I run your command, it gives no output, But I was looking for a output like this :

Code:
process1: 0
process2: 0
process3: 0

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Check processes running on remote server

Hello Guys, I need some help to find out if processes are running on remote server or not. I could do 'ssh' to do that but due to some security reasons, I need to avoid the ssh & get result from remote server. Could you please suggest some that can be done without ssh or similar sort of... (8 Replies)
Discussion started by: UnknownGuy
8 Replies

2. UNIX for Beginners Questions & Answers

How to check the processes running longer than 2 hours.?

HI can someone help me to check the process running more than 2 hours. I have the below command which shows the time and process id, however, I only need the processes running more than 2 hours. (8 Replies)
Discussion started by: Vinod
8 Replies

3. UNIX for Beginners Questions & Answers

Many processes running at the same time

Hello everybody , I launched cron to execute a task every hour but the job takes more than hour that's why I'm getting more than 1000 cron processes running at the same time !!! My question is how to tell cron not to execute unless the job terminated in order to have only one process running .... (14 Replies)
Discussion started by: beautymind
14 Replies

4. AIX

Need to check long running processes on the database server and the os is AIX

Hello, Please help me with a script with which I can check long running processes on the database server and the os is AIX. Best regards, Vishal (5 Replies)
Discussion started by: Vishal_dba
5 Replies

5. Linux

Running processes

Hi guys is it normal to have 5-10 cron/syslog processes running... in my case i got 10 cron process running. (4 Replies)
Discussion started by: batas
4 Replies

6. Solaris

Running processes on GZ/LZ

Hi guys just a question is it normal to see running process on a non-global zone in the global zone... processes such as cron. (3 Replies)
Discussion started by: batas
3 Replies

7. Shell Programming and Scripting

how to know the running processes.

Hi can anybody help me regarding this.. i want know the output of ps -ef with explanation. how can we know the running processess. this is the output of ps -elf F S UID PID PPID C PRI NI ADDR SZ WCHAN STIME TTY TIME CMD 19 T root 0 0 0 0 SY ... (1 Reply)
Discussion started by: rajesh_pola
1 Replies

8. UNIX for Dummies Questions & Answers

How to check the status of the processes running for the current user?

Hi All, I am new to unix. Can anyone tell me "How to check the status of the processes running for the current user?" Regards, Ravindaran S (1 Reply)
Discussion started by: ravind27
1 Replies

9. Shell Programming and Scripting

Script to check running processes on remote server.

Hi, I am trying to write a script, which queries a db to get the names of processes, stores it in a file and then checks if that process is running on a remote server. However I am not getting it right, could anyone help me out. #!/bin/sh echo "select Address from Device where Cust =... (5 Replies)
Discussion started by: amitsayshii
5 Replies

10. UNIX for Dummies Questions & Answers

how to find all processes that are running

Hi i've been googling a lot but can't find an answer. All I would like to know is how to find out all processes that are running on a machine. I know ps gives all YOUR processes. thanks (9 Replies)
Discussion started by: speedieB
9 Replies
Login or Register to Ask a Question