How to check same process running 10 times?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to check same process running 10 times?
# 1  
Old 11-09-2017
How to check same process running 10 times?

At run time Without knowing job name how to check the job running in specific user "ABCD" ,If the running job duplicate more then 10 then script it self send alert message to the users with the process ID name so that will kill the processed to avoid hung issue ,tried below script please check and let me know if any comments

1)Not able to add line to check job running duplicate more then 10 times
HTML Code:
 
ps -fu ABCD | awk '{print $1 $2,$8}' | \
     while read pid ppid
     do
           
           then                
             mail -s "Please kill the process " "kalia@gmail.com"
           fi
             
     done

Last edited by Kalia; 11-09-2017 at 11:50 AM..
# 2  
Old 11-09-2017
We cant reply if you spend your time modifying your code, our posts risk beeing off topic you already modifies 3 times - I started to reply to the previous and found the code changed again making my reply completely ununderstandable
# 3  
Old 11-09-2017
Here is an approach using pgrep and awk that will check duplicate instances by process name:-
Code:
pgrep -l -U ABCD | awk '
        {
                ++p_counter[$2]
        }
        END {
                for ( k in p_counter )
                {
                        if ( p_counter[k] > 10 )
                        {
                                cmd = "echo \"Found " p_counter[k] " instances of " k " running\" | mailx -s \"Please kill process\" user@gmail.com"
                                system(cmd)
                        }
                }
        }
'

# 4  
Old 11-09-2017
Assumption: You are using bash on Linux. Try something like:
Code:
while read count name
do
   if (( count >= 10 ))
   then
   # email command goes here
   fi
done <(ps -u ABCD -ocomm= | sort | uniq -c)

Untested.

Basically don't use ps -f | awk 'print $x' when you can use ps -o xxx
Sort puts the commands into order; uniq -c counts them.

Andrew
This User Gave Thanks to apmcd47 For This Post:
# 5  
Old 11-09-2017
Note that ps -o xxx will not work on all systems, E.g. HP-UX

You have to set UNIX95 flag to obey the XPG4 - XOpen Portibilty Guide V4
Code:
UNIX95=1 ps -u ABCD-ocomm=

This User Gave Thanks to Yoda For This Post:
# 6  
Old 11-09-2017
Thanks for urs replay

Am attaching sample input file so that you can clearly understand

Output send below alert message

"The job "d.ksh" runing more then 10 time please check the process avoid hung issue"
"The job "a.ksh" runing more then 10 time please check the process avoid hung issue"
if we can addd job name with processid in the script that will be also fine

below is input file

Code:
abcd       351 0.0  0.0  848    0 q9 IW   May  2  0:46 /usr/local/ksh/d.ksh
abcd       352 0.0  0.0  848    0 q9 IW   May  2  0:46 /usr/local/ksh/d.ksh
abcd       3533  0.0  0.0  848    0 q9 IW   May  2  0:46 /usr/local/ksh/d.ksh
abcd       3554  0.0  0.0  848    0 q9 IW   May  2  0:46 /usr/local/ksh/d.ksh
abcd       3655  0.0  0.0  848    0 q9 IW   May  2  0:46 /usr/local/ksh/d.ksh
abcd       356  0.0  0.0  848    0 q9 IW   May  2  0:46 /usr/local/ksh/d.ksh
abcd       357  0.0  0.0  848    0 q9 IW   May  2  0:46 /usr/local/ksh/d.ksh
abcd       36  0.0  0.0  848    0 q9 IW   May  2  0:46 /usr/local/ksh/d.ksh
abcd       359  0.0  0.0  848    0 q9 IW   May  2  0:46 /usr/local/ksh/d.ksh
abcd       3599  0.0  0.0  848    0 q9 IW   May  2  0:46 /usr/local/ksh/d.ksh
abcd       3499  0.0  0.0  848    0 q9 IW   May  2  0:46 /usr/local/ksh/d.ksh
abcd     24776  0.0  0.0  836    0 q0 IW   May  2  0:41 /usr/local/bin/pine3.96
abcd       1485  0.0  1.8  624  540 q2 S    18:33   0:07 /usr/local/bin/pine3.96
abcd       2280  0.0  0.0  692    0 p7 IW   18:51   0:05 /usr/local/bin/pine3.96
abcd      2737  0.0  0.7   32  204 q4 S    19:00   0:00 grep pine
abcd      1690  0.0  5.3  696 1616 p8 S    18:39   0:09 /usr/local/bin/pine3.96
abcd       1  0.0  848    0 q9 IW   May  2  0:46 /usr/local/kali/ksh/a.ksh
abcd       2 0.0  0.0  848    0 q9 IW   May  2  0:46 /usr/local/kali/ksh/a.ksh
abcd       3  0.0  0.0  848    0 q9 IW   May  2  0:46 /usr/local/kali/ksh/a.ksh
abcd       4  0.0  0.0  848    0 q9 IW   May  2  0:46 /usr/local/kali/ksh/a.ksh
abcd       5  0.0  0.0  848    0 q9 IW   May  2  0:46 /usr/local/kali/ksh/a.ksh
abcd       6  0.0  0.0  848    0 q9 IW   May  2  0:46 /usr/local/kali/ksh/a.ksh
abcd       7  0.0  0.0  848    0 q9 IW   May  2  0:46 /usr/local/kali/ksh/a.ksh
abcd       8  0.0  0.0  848    0 q9 IW   May  2  0:46 /usr/local/kali/ksh/a.ksh
abcd       9  0.0  0.0  848    0 q9 IW   May  2  0:46 /usr/local/kali/ksh/a.ksh
abcd       91  0.0  0.0  848    0 q9 IW   May  2  0:46 /usr/local/kali/ksh/a.ksh
abcd       95  0.0  0.0  848    0 q9 IW   May  2  0:46 /usr/local/kali/ksh/a.ksh
abcd       3599  0.0  0.0  848    0 q9 IW   May  2  0:46 /usr/local/ksh/d.ksh
abcd     24776  0.0  0.0  836    0 q0 IW   May  2  0:41 /usr/local/bin/pine3.96
abcd       1485  0.0  1.8  624  540 q2 S    18:33   0:07 /usr/local/bin/pine3.96
abcd       2280  0.0  0.0  692    0 p7 IW   18:51   0:05 /usr/local/bin/pine3.96
abcd      2737  0.0  0.7   32  204 q4 S    19:00   0:00 grep pine
abcd      1690  0.0  5.3  696 1616 p8 S    18:39   0:09 /usr/local/bin/pine3.96


Last edited by Corona688; 11-09-2017 at 02:17 PM..
# 7  
Old 11-09-2017
Please use code tags for posting code fragments or data samples.

Replace comm with args in apmcd47 solution:-
Code:
UNIX95=1 ps -u ABCD -oargs=

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

How a process can check if a particular process is running on different machine?

I have process1 running on one machine and generating some log file. Now another process which can be launched on any machine wants to know if process1 is running or not and also in case it is running it wants to stream the logs file generated by process1 on terminal from which process2 is... (2 Replies)
Discussion started by: saurabhnsit2001
2 Replies

2. Shell Programming and Scripting

need to check if the process is running

Hi, I check if the process is running or not using the below. /usr/ucb/ps auxww | grep 109 |grep rmi | awk '{print $2}' 9718 Thus we see 9718 is the PID. It return blank if the process is not running. I need to perform some action if the process is not running and leave it if... (8 Replies)
Discussion started by: shifahim
8 Replies

3. Shell Programming and Scripting

Script to check running of process

Hi, Can anyone please tell me how to write a shell script to check whether a process if running or not.... if its still running then wait for sometime and if not then run the next query. Also, Under my one main script main.sh I have to run 2 scripts simutaneously which take some time to... (2 Replies)
Discussion started by: lovepujain
2 Replies

4. Programming

How do I check if a process is running in C

How to I check if a process is running in C? I'm trying to use ps aux |grep "process name" but failing in doing that. How do I do that? Thanks, (1 Reply)
Discussion started by: cprogdude
1 Replies

5. UNIX and Linux Applications

How to check if process is running?

Hi I would like to check if an instance of a script is already running. I've seen many different examples, but I haven't the slightest idea as to how to do this. Can you please help. Thank you. (5 Replies)
Discussion started by: ladyAnne
5 Replies

6. Shell Programming and Scripting

Check if Process is running

Hi , I have a csh code below which check the process if it's running. Can any expert advise me on the following: 1) what does this notationmean ">!" and how is it different from the append ">" notation ? 2) how does "setenv" work in this code ? # Check whether there is a running... (3 Replies)
Discussion started by: Raynon
3 Replies

7. UNIX for Advanced & Expert Users

How to check since when (for how long) a particular process is running ?

How do I check if a particular process is running from a certain date and time ? (2 Replies)
Discussion started by: hpuxlxboy
2 Replies

8. Shell Programming and Scripting

script to check if process is running

How do I make a shell script to see if a certain process is running. The process shows up on ps aux as /usr/sbin/daemon eg: if /usr/sbin/daemon else #nothin basically i want to run the process if it isnt running/ has been stopped. Thanks. (2 Replies)
Discussion started by: daydreamer
2 Replies

9. Solaris

Check whether ftpd process is running or not?

Hi, I want to check whether the ftpd process is running or not. Is there any solaris command that can help me in this? Or is there any other way in which I can check this? Is it possible to check programmatically (preferrably in a C++ code)? Eagerly awaiting a reply. Thanx. (8 Replies)
Discussion started by: The.White.Rider
8 Replies

10. Shell Programming and Scripting

check process running

I have this script: ------------------------------------------------------- #!/bin/ksh # if ] || ] then echo "Executing main_load.sh script" /usr1/psc_load/jobs/cron/main_load.sh "ods" else echo "File not found, do nothing" fi exit 0 ... (4 Replies)
Discussion started by: rose1207
4 Replies
Login or Register to Ask a Question