Results 1 to 10 of 10
Hi all,
I need help on bash script to do some specific process selection.
I know that we can actually get pid from process name by using
ps -C processname ...
- 04-08-2011 #1Just Joined!
- Join Date
- Apr 2011
- Posts
- 6
[SOLVED] Need help on bash script process selection
Hi all,
I need help on bash script to do some specific process selection.
I know that we can actually get pid from process name by using
but I don't know how to deal with my condition below:ps -C processname -o pid=
I have some processes running on my system:
Code:Damn, I can't past my ps result here as it keeps giving me error when I try to submit it "You are only allowed to post URLs to other sites after you have made 15 posts or more."
I need to get a script which can give me only the pid (in this case = 17005) of the process "827715".
If I used "-C bash" then it will return not only the one which I need but also another process (pid = 1704
which I don't need. If I specify "-c bash /opt/sge-6.2u4/default/spool/east-25/job_scripts/827715" it doesn't give any output to me.
Any idea how do deal with it?
Thanks.Last edited by ampra2; 04-08-2011 at 02:40 PM.
- 04-08-2011 #2Just Joined!
- Join Date
- Apr 2011
- Posts
- 6
Hope this time it works:
This is the ps result:
Code:ampra2 17005 0.0 0.0 65932 1332 ? Ss 22:02 0:00 -bash /opt/sge-6.2u4/default/spool/east-25/job_scripts/827715 ampra2 17044 100 0.0 33984 1556 ? R 22:02 0:15 openssl speed root 17045 0.0 0.0 94448 3520 ? Ss 22:02 0:00 sshd: ampra2 [priv] ampra2 17047 0.0 0.0 94448 1764 ? S 22:02 0:00 sshd: ampra2@pts/0 ampra2 17048 0.1 0.0 66200 1600 pts/0 Ss 22:02 0:00 -bash ampra2 17089 0.0 0.0 65596 968 pts/0 R+ 22:02 0:00 ps aux ampra2 17090 0.0 0.0 61156 736 pts/0 S+ 22:02 0:00 grep ampra2
- 04-08-2011 #3
May I suggest to let the called script write a pidfile into /var/run/<process>/<process>.pid?
This way you avoid parsing ps output (and hope for a match)
and you also gain some portability, as ps is not the same on each unix platform.You must always face the curtain with a bow.
- 04-08-2011 #4Just Joined!
- Join Date
- Apr 2011
- Posts
- 6
Hi Irithori,
Thanks for your reply, but the called script is actually a job submitted into a grid, so I can do nothing with it.
On that case, 827715 is the JobID of the job submitted on the head node, this is what I can use to search for the right process when the job is executed on the computing node.
Any idea how to get its pid with this condition?
Thanks.
- 04-08-2011 #5
I'm not sure if I grasp this. You submit a job to a grid and that job executes somewhere .. and you want to get the pid for it on your pc? Isn't that ... impossible unless you ssh into the machine that really executes the job? When I last dealed with grid computing there were several well defined tools to place jobs, retrieve the status of a job and finally also retrieve the results.
Anyway, you might consider taking a closer look at the manual pages of ps (man ps), especially the options -C and -o.
- 04-08-2011 #6Just Joined!
- Join Date
- Apr 2011
- Posts
- 6
Hi Kloschüssel,
It's a bit complicated to explain. To make it short, I'm in the middle of building a process monitoring tool to work on grid. To do that, I need to get the pid of the job when it's running on the node.
You don't have to care about the grid thing actually, I just need to know the way I can get pid of a specific process with only knowing the name of the script being executed.
I can do multiple grep like this which will direct me to the correct process.
but it gives me a long output, as I can't use "-o pid=" option on "ps" which can be used to limit the output only on pid.Code:ps ax | grep -v "grep" | grep 827899 | grep job_scripts 17005 ? Ss 0:00 -bash /opt/sge-6.2u4/default/spool/east-26/job_scripts/827715
It means I have to do another extra job by parsing this output to get only the pid.
I'm wondering whether there is another easy way to get only the PID.
- 04-08-2011 #7
I also miss the big picture here.
But given, that one would locally look for a pid, that has a process name of bash and "827715" somewhere in the comandline, then this should do.
And maybe can be matched much closer.
Code:pgrep -f "bash(.*)827715"
You must always face the curtain with a bow.
- 04-08-2011 #8
- 04-08-2011 #9
Please read the manual pages, there's an example telling "Print only the process IDs of syslogd".
EDIT: pgrep is new to me ..
cool!
- 04-08-2011 #10Just Joined!
- Join Date
- Apr 2011
- Posts
- 6



