Unix/Linux Go Back    


Shell Programming and Scripting BSD, Linux, and UNIX shell scripting — Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

How to check same process running 10 times?

Shell Programming and Scripting


Reply    
 
Thread Tools Search this Thread Display Modes
    #8  
Old Unix and Linux 4 Weeks Ago   -   Original Discussion by Kalia
Kalia's Unix or Linux Image
Kalia Kalia is offline
Registered User
 
Join Date: Jun 2017
Last Activity: 6 December 2017, 3:20 PM EST
Posts: 28
Thanks: 6
Thanked 0 Times in 0 Posts
AIX-Am new in ksh script While runing both the script getting below errors

Code:
syntax error near unexpected token `-u'

awk: cmd. line:10:                                 cmd = "echo \"Found " p_counter[k] " instances of " k " running\"
awk: cmd. line:10:                                                                                       ^ unterminated string

---------- Post updated at 12:38 AM ---------- Previous update was at 12:07 AM ----------

tried below script want to add one more step to add duplicated job name at run time

Code:
cnt=$(ps -u abcd | )
if [[ $cnt -gt 1 ]]; then
  echo "job runing longtime"
  exit 1
fi;

Sponsored Links
    #9  
Old Unix and Linux 4 Weeks Ago   -   Original Discussion by Kalia
MadeInGermany's Unix or Linux Image
MadeInGermany MadeInGermany is offline Forum Staff  
Moderator
 
Join Date: May 2012
Last Activity: 12 December 2017, 2:37 AM EST
Location: Simplicity
Posts: 3,862
Thanks: 320
Thanked 1,289 Times in 1,167 Posts
Do you see it now?

Code:
cmd = "echo \"Found " p_counter[k] " instances of " k " running\" 
                                                                                             ^ unterminated string

The following reports all processes from user abcd with 10 or more instances

Code:
ps -u abcd -o comm= | awk '++s[$1]==10'

The following also filters for process name d.ksh or a.ksh

Code:
ps -u abcd -o comm= | awk '($1=="a.ksh" || $1=="d.ksh") && ++s[$1]==10'

Sponsored Links
    #10  
Old Unix and Linux 4 Weeks Ago   -   Original Discussion by Kalia
Kalia's Unix or Linux Image
Kalia Kalia is offline
Registered User
 
Join Date: Jun 2017
Last Activity: 6 December 2017, 3:20 PM EST
Posts: 28
Thanks: 6
Thanked 0 Times in 0 Posts
Thanks for replay

yes it is working fine please let me know below syntax

2..Also if you can add to send alert massage with running job name more then 10 time with their process ID,Will be fine


Code:
 -o comm=

    #11  
Old Unix and Linux 4 Weeks Ago   -   Original Discussion by Kalia
MadeInGermany's Unix or Linux Image
MadeInGermany MadeInGermany is offline Forum Staff  
Moderator
 
Join Date: May 2012
Last Activity: 12 December 2017, 2:37 AM EST
Location: Simplicity
Posts: 3,862
Thanks: 320
Thanked 1,289 Times in 1,167 Posts
You can test the output for being empty.
For example you can store it in a varable, and test that for being empty (-z Here: -n not empty).

Code:
jobs=$(ps -u abcd -o comm= | awk '($1=="a.ksh" || $1=="d.ksh") && ++s[$1]==10')
if [[ -n "$jobs" ]]
then
  printf "jobs runing longtime:\n%s\n" "$jobs"
  exit 1
fi

Sponsored Links
    #12  
Old Unix and Linux 4 Weeks Ago   -   Original Discussion by Kalia
apmcd47's Unix or Linux Image
apmcd47 apmcd47 is offline
Registered User
 
Join Date: Feb 2011
Last Activity: 12 December 2017, 11:23 AM EST
Posts: 283
Thanks: 13
Thanked 76 Times in 73 Posts
Quote:
Originally Posted by Kalia View Post
Thanks for replay

yes it is working fine please let me know below syntax

2..Also if you can add to send alert massage with running job name more then 10 time with their process ID,Will be fine


Code:
 -o comm=

Basically the -o switch to ps allows you to select your own fields rather than use the defaults. This is particularly important in writing scripts that may be transferred to another platform, because the default selection of fields in ps -ef will be different if you move from Linux to Unix or MacOS X but ps -o will, with Yoda's caveats, most likely work. And if they don't, you will get an error rather than some odd misbehaviour that is hard to trace.

Multiple fields can be specified using a comma to separate each from the next. For instance, the default fields given by
Code:
ps

on my Ubuntu system are equivalent to
Code:
ps -opid,tty,time,comm

but if I wanted the parent-pid and effective user-id instead of the tty and the time I could type
Code:
ps -opid,ppid,euid,comm

And by changing the order of the field names you change their order in the output.

Appending the equals sign (=) to a field name suppresses its heading in the final output. Note, though, that you have to suppress the heading of every field, for example:

Code:
ps -opid=,ppid=,args=  # right
ps -opid,ppid,args=    # wrong, will only suppress the COMMAND heading

I hope that makes sense.

Getting the PIDs of the processes in the alert could be more tricky. If you use my original suggestion and have pgrep on your system you could use pgrep within the loop to inject the PIDs into your message.

Andrew
The Following User Says Thank You to apmcd47 For This Useful Post:
MadeInGermany (4 Weeks Ago)
Sponsored Links
    #13  
Old Unix and Linux 4 Weeks Ago   -   Original Discussion by Kalia
Kalia's Unix or Linux Image
Kalia Kalia is offline
Registered User
 
Join Date: Jun 2017
Last Activity: 6 December 2017, 3:20 PM EST
Posts: 28
Thanks: 6
Thanked 0 Times in 0 Posts
Thanks to all ,MadeInGermany

for below script below script matching my requirement.
doubt- is it possible to print job with process ID if not please ignore

Code:
  
 jobs=$(ps -u abcd -o comm= | awk '($1=="a.ksh" || $1=="d.ksh") && ++s[$1]==10')
if [[ -n "$jobs" ]]
then
  printf "jobs runing longtime:\n%s\n" "$jobs"
  exit 1
fi

---------- Post updated at 11:41 PM ---------- Previous update was at 07:42 PM ----------

HI I have almost near to my requirement
getting small issue need some correction while runing below script not fetching current runing full jobs name more then 10times

Code:
ps -u abcd -o comm= | awk '++s[$1]==10'

for example: runing more then time 10 time 3 different paths

Code:
/usr/local/ksh/d.ksh 
/usr/ka/ksh/ab.java 
/usr/na/ksh/abc.perl

For my server runing more then 100 jobs on different location/path using AIX operating system
While runing its fetching only ksh ,perl.java,c,pli like that not fetching full name of the jobs

Last edited by Kalia; 4 Weeks Ago at 02:17 PM..
Sponsored Links
    #14  
Old Unix and Linux 4 Weeks Ago   -   Original Discussion by Kalia
MadeInGermany's Unix or Linux Image
MadeInGermany MadeInGermany is offline Forum Staff  
Moderator
 
Join Date: May 2012
Last Activity: 12 December 2017, 2:37 AM EST
Location: Simplicity
Posts: 3,862
Thanks: 320
Thanked 1,289 Times in 1,167 Posts
Sure, you can add another -o pid= column, and you can change -o comm= to -o args=
Rule: put the predictable columns first, and have the args last!

Code:
ps -u abcd -o pid= -o args=

The trailing = means "don't print a header for this column", and all columns = means "don't print a header line at all".
apmcd47 mentioned a shorter notation but I found it does not work everywhere.
Does it look more promising now?
Postprocessing of args is problematic because it contains spaces. In awk you cannot simply refer to column $2 because your process name might be in $3...
Sponsored Links
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
How a process can check if a particular process is running on different machine? saurabhnsit2001 UNIX for Dummies Questions & Answers 2 06-03-2012 12:14 AM
need to check if the process is running shifahim Shell Programming and Scripting 8 08-18-2011 11:17 PM
How do I check if a process is running in C cprogdude Programming 1 10-12-2010 06:01 AM
How to check if process is running? ladyAnne UNIX and Linux Applications 5 05-01-2010 02:20 AM
Check if Process is running Raynon Shell Programming and Scripting 3 01-08-2010 03:03 AM



All times are GMT -4. The time now is 01:59 PM.