How to count the number of instances of a certain process by canonical path (with arguments)?
Let's say I run the following commands:
sleep 500
/bin/sleep 500
sleep 30
What I'm interested is, how to count the number of instances of the sleep program, with certain arguments (in this case the only argument is 500
).
So in the example above, if I count the number of instances of /bin/sleep 500
, it should return 2.
I tried this: pgrep -xfc '/bin/sleep 500'
, but since it exactly matched the argument in the parenthesis, sleep 500
isn't counted.
ps
add a comment |
Let's say I run the following commands:
sleep 500
/bin/sleep 500
sleep 30
What I'm interested is, how to count the number of instances of the sleep program, with certain arguments (in this case the only argument is 500
).
So in the example above, if I count the number of instances of /bin/sleep 500
, it should return 2.
I tried this: pgrep -xfc '/bin/sleep 500'
, but since it exactly matched the argument in the parenthesis, sleep 500
isn't counted.
ps
Are you running a system with a/proc
filesystem? That makes it easier to find the pathname of each process's executable.
– Mark Plotnick
May 25 '16 at 18:13
@MarkPlotnick Yes, I am
– mythic
May 25 '16 at 18:49
add a comment |
Let's say I run the following commands:
sleep 500
/bin/sleep 500
sleep 30
What I'm interested is, how to count the number of instances of the sleep program, with certain arguments (in this case the only argument is 500
).
So in the example above, if I count the number of instances of /bin/sleep 500
, it should return 2.
I tried this: pgrep -xfc '/bin/sleep 500'
, but since it exactly matched the argument in the parenthesis, sleep 500
isn't counted.
ps
Let's say I run the following commands:
sleep 500
/bin/sleep 500
sleep 30
What I'm interested is, how to count the number of instances of the sleep program, with certain arguments (in this case the only argument is 500
).
So in the example above, if I count the number of instances of /bin/sleep 500
, it should return 2.
I tried this: pgrep -xfc '/bin/sleep 500'
, but since it exactly matched the argument in the parenthesis, sleep 500
isn't counted.
ps
ps
edited 8 mins ago
Rui F Ribeiro
40.1k1479136
40.1k1479136
asked May 25 '16 at 16:29
mythicmythic
1435
1435
Are you running a system with a/proc
filesystem? That makes it easier to find the pathname of each process's executable.
– Mark Plotnick
May 25 '16 at 18:13
@MarkPlotnick Yes, I am
– mythic
May 25 '16 at 18:49
add a comment |
Are you running a system with a/proc
filesystem? That makes it easier to find the pathname of each process's executable.
– Mark Plotnick
May 25 '16 at 18:13
@MarkPlotnick Yes, I am
– mythic
May 25 '16 at 18:49
Are you running a system with a
/proc
filesystem? That makes it easier to find the pathname of each process's executable.– Mark Plotnick
May 25 '16 at 18:13
Are you running a system with a
/proc
filesystem? That makes it easier to find the pathname of each process's executable.– Mark Plotnick
May 25 '16 at 18:13
@MarkPlotnick Yes, I am
– mythic
May 25 '16 at 18:49
@MarkPlotnick Yes, I am
– mythic
May 25 '16 at 18:49
add a comment |
3 Answers
3
active
oldest
votes
In your example you can go with:
pgrep -fc 'sleep 500'
It matches both /bin/sleep 500
and sleep 500
.
Or if you want to be more precise:
pgrep -fc 'sleep 500$'
add a comment |
On GNU system:
$ ps --no-header -C sleep -o args | grep -Ec ' 500( |$)'
2
add a comment |
On systems that support a Linux-like /proc
:
#!/bin/sh
if [ $# != 2 ]
then
echo usage: "$0" pathname commandline_regexp
exit 1
fi
cd /proc
for p in [0-9]*
do
exe=$(readlink $p/exe 2>/dev/null)
if [ "$exe" = "$1" ] &&
cat $p/cmdline 2>/dev/null | tr '' ' ' | grep -q -- "$2"
then
echo match $p
fi
done
example:
$ sleep 500&
[3] 18280
$ sleep 600&
[4] 18281
$ ./rpgrep /bin/sleep '.*sleep 500 $'
match 18280
$ ./rpgrep /bin/sleep '.*sleep.*00'
match 18280
match 18281
Notes:
- the
2>/dev/null
and the separatecat
process are used to cope with the possibility that processes may disappear while the script is running.
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "106"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f285453%2fhow-to-count-the-number-of-instances-of-a-certain-process-by-canonical-path-wit%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
In your example you can go with:
pgrep -fc 'sleep 500'
It matches both /bin/sleep 500
and sleep 500
.
Or if you want to be more precise:
pgrep -fc 'sleep 500$'
add a comment |
In your example you can go with:
pgrep -fc 'sleep 500'
It matches both /bin/sleep 500
and sleep 500
.
Or if you want to be more precise:
pgrep -fc 'sleep 500$'
add a comment |
In your example you can go with:
pgrep -fc 'sleep 500'
It matches both /bin/sleep 500
and sleep 500
.
Or if you want to be more precise:
pgrep -fc 'sleep 500$'
In your example you can go with:
pgrep -fc 'sleep 500'
It matches both /bin/sleep 500
and sleep 500
.
Or if you want to be more precise:
pgrep -fc 'sleep 500$'
edited May 25 '16 at 21:56
answered May 25 '16 at 21:50
lgagginilgaggini
41026
41026
add a comment |
add a comment |
On GNU system:
$ ps --no-header -C sleep -o args | grep -Ec ' 500( |$)'
2
add a comment |
On GNU system:
$ ps --no-header -C sleep -o args | grep -Ec ' 500( |$)'
2
add a comment |
On GNU system:
$ ps --no-header -C sleep -o args | grep -Ec ' 500( |$)'
2
On GNU system:
$ ps --no-header -C sleep -o args | grep -Ec ' 500( |$)'
2
answered May 25 '16 at 17:08
cuonglmcuonglm
104k24203302
104k24203302
add a comment |
add a comment |
On systems that support a Linux-like /proc
:
#!/bin/sh
if [ $# != 2 ]
then
echo usage: "$0" pathname commandline_regexp
exit 1
fi
cd /proc
for p in [0-9]*
do
exe=$(readlink $p/exe 2>/dev/null)
if [ "$exe" = "$1" ] &&
cat $p/cmdline 2>/dev/null | tr '' ' ' | grep -q -- "$2"
then
echo match $p
fi
done
example:
$ sleep 500&
[3] 18280
$ sleep 600&
[4] 18281
$ ./rpgrep /bin/sleep '.*sleep 500 $'
match 18280
$ ./rpgrep /bin/sleep '.*sleep.*00'
match 18280
match 18281
Notes:
- the
2>/dev/null
and the separatecat
process are used to cope with the possibility that processes may disappear while the script is running.
add a comment |
On systems that support a Linux-like /proc
:
#!/bin/sh
if [ $# != 2 ]
then
echo usage: "$0" pathname commandline_regexp
exit 1
fi
cd /proc
for p in [0-9]*
do
exe=$(readlink $p/exe 2>/dev/null)
if [ "$exe" = "$1" ] &&
cat $p/cmdline 2>/dev/null | tr '' ' ' | grep -q -- "$2"
then
echo match $p
fi
done
example:
$ sleep 500&
[3] 18280
$ sleep 600&
[4] 18281
$ ./rpgrep /bin/sleep '.*sleep 500 $'
match 18280
$ ./rpgrep /bin/sleep '.*sleep.*00'
match 18280
match 18281
Notes:
- the
2>/dev/null
and the separatecat
process are used to cope with the possibility that processes may disappear while the script is running.
add a comment |
On systems that support a Linux-like /proc
:
#!/bin/sh
if [ $# != 2 ]
then
echo usage: "$0" pathname commandline_regexp
exit 1
fi
cd /proc
for p in [0-9]*
do
exe=$(readlink $p/exe 2>/dev/null)
if [ "$exe" = "$1" ] &&
cat $p/cmdline 2>/dev/null | tr '' ' ' | grep -q -- "$2"
then
echo match $p
fi
done
example:
$ sleep 500&
[3] 18280
$ sleep 600&
[4] 18281
$ ./rpgrep /bin/sleep '.*sleep 500 $'
match 18280
$ ./rpgrep /bin/sleep '.*sleep.*00'
match 18280
match 18281
Notes:
- the
2>/dev/null
and the separatecat
process are used to cope with the possibility that processes may disappear while the script is running.
On systems that support a Linux-like /proc
:
#!/bin/sh
if [ $# != 2 ]
then
echo usage: "$0" pathname commandline_regexp
exit 1
fi
cd /proc
for p in [0-9]*
do
exe=$(readlink $p/exe 2>/dev/null)
if [ "$exe" = "$1" ] &&
cat $p/cmdline 2>/dev/null | tr '' ' ' | grep -q -- "$2"
then
echo match $p
fi
done
example:
$ sleep 500&
[3] 18280
$ sleep 600&
[4] 18281
$ ./rpgrep /bin/sleep '.*sleep 500 $'
match 18280
$ ./rpgrep /bin/sleep '.*sleep.*00'
match 18280
match 18281
Notes:
- the
2>/dev/null
and the separatecat
process are used to cope with the possibility that processes may disappear while the script is running.
edited May 26 '16 at 14:21
answered May 25 '16 at 19:43
Mark PlotnickMark Plotnick
18.2k24065
18.2k24065
add a comment |
add a comment |
Thanks for contributing an answer to Unix & Linux Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f285453%2fhow-to-count-the-number-of-instances-of-a-certain-process-by-canonical-path-wit%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Are you running a system with a
/proc
filesystem? That makes it easier to find the pathname of each process's executable.– Mark Plotnick
May 25 '16 at 18:13
@MarkPlotnick Yes, I am
– mythic
May 25 '16 at 18:49