Excluding grep from process list
up vote
26
down vote
favorite
I have cobbled together a command to return the process ID of a running daemon:
ps aux | grep daemon_name | awk "{ print $2 }"
It works perfectly and returns the PID, but it also returns a second PID which is presumably the process I'm running now. Is there a way I can exclude my command from the list of returned PIDs?
I've tested it a few times and it appears my command is always the second PID in the list, but I don't want to grab just the first PID in case it's inaccurate.
grep ps pid
add a comment |
up vote
26
down vote
favorite
I have cobbled together a command to return the process ID of a running daemon:
ps aux | grep daemon_name | awk "{ print $2 }"
It works perfectly and returns the PID, but it also returns a second PID which is presumably the process I'm running now. Is there a way I can exclude my command from the list of returned PIDs?
I've tested it a few times and it appears my command is always the second PID in the list, but I don't want to grab just the first PID in case it's inaccurate.
grep ps pid
serverfault.com/questions/367921/…
– Der Hochstapler
Apr 7 '12 at 12:29
add a comment |
up vote
26
down vote
favorite
up vote
26
down vote
favorite
I have cobbled together a command to return the process ID of a running daemon:
ps aux | grep daemon_name | awk "{ print $2 }"
It works perfectly and returns the PID, but it also returns a second PID which is presumably the process I'm running now. Is there a way I can exclude my command from the list of returned PIDs?
I've tested it a few times and it appears my command is always the second PID in the list, but I don't want to grab just the first PID in case it's inaccurate.
grep ps pid
I have cobbled together a command to return the process ID of a running daemon:
ps aux | grep daemon_name | awk "{ print $2 }"
It works perfectly and returns the PID, but it also returns a second PID which is presumably the process I'm running now. Is there a way I can exclude my command from the list of returned PIDs?
I've tested it a few times and it appears my command is always the second PID in the list, but I don't want to grab just the first PID in case it's inaccurate.
grep ps pid
grep ps pid
asked Apr 7 '12 at 12:12
tak
133124
133124
serverfault.com/questions/367921/…
– Der Hochstapler
Apr 7 '12 at 12:29
add a comment |
serverfault.com/questions/367921/…
– Der Hochstapler
Apr 7 '12 at 12:29
serverfault.com/questions/367921/…
– Der Hochstapler
Apr 7 '12 at 12:29
serverfault.com/questions/367921/…
– Der Hochstapler
Apr 7 '12 at 12:29
add a comment |
7 Answers
7
active
oldest
votes
up vote
26
down vote
accepted
grep's -v
switch reverses the result, excluding it from the queue. So make it like:
ps aux | grep daemon_name | grep -v "grep daemon_name" | awk "{ print $2 }"
Upd. You can also use -C
switch to specify command name like so:
ps -C daemon_name -o pid=
The latter -o
determines which columns of the information you want in the listing. pid
lists only the process id column. And the equal sign =
after pid
means there will be no column title for that one, so you get only the clear numbers - PID's.
Hope this helps.
2
Using "grep -v grep": Just watch out that you may have a long command line including "grep" (Let's say somebody wrote a grep library=> "-Llibgrep.so" or "-cp libgrep.jar". At least I would repeat the pattern : grep -v "grep daemon_name"
– phil_w
Apr 9 '15 at 15:01
Good point, @phil_w, thanks. Updated the answer.
– Serg ikS
Apr 10 '15 at 7:50
add a comment |
up vote
23
down vote
You can use a character class trick. "[d]" does not match "[d]" only "d".
ps aux | grep [d]aemon_name | awk "{ print $2 }"
I prefer this to using | grep -v grep
.
Heh that's a clever trick, I like it. I'm keeping things simple at the moment though while I'm learning, and| grep -v grep
will be easier to understand when I revisit my code.
– tak
Apr 7 '12 at 12:29
I am trying to understand how this [h]ack works. Could you please explain what you mean with"[d]" does not match "[d]" only "d"
?
– Nowaker
Sep 28 '13 at 21:13
@Damian: The square brackets are metacharacters (delimiting a set of characters) in the search expression so thegrep
command won't match theps
output line for thegrep
command itself (what tak called "a second PID" in the original question). expressiona[xy]d
does not match texta[xy]d
but it does match textaxd
or textayd
. Hence expression[f]oo
matches textfoo
but not text[f]oo
. I hope that is clear.
– RedGrittyBrick
Sep 28 '13 at 21:34
Okay, I was wrong about ARGV. This is just a regexp. Thanks.
– Nowaker
Sep 29 '13 at 1:29
Devilishly clever! Had to think about it for a moment -- thanks!
– Subfuzion
Oct 23 '13 at 18:46
|
show 4 more comments
up vote
15
down vote
Avoid parsing ps
's output if there are more reliable alternatives.
pgrep daemon_name
pidof daemon_name
Yay, finally process management makes sense now.
– Błażej Michalik
Jun 2 '16 at 6:09
pgrep
searches by substring whilepidof
by exact match - exaclty what I needed! thanks!
– Sasha
Aug 19 '16 at 11:14
add a comment |
up vote
3
down vote
The ps -C
option is not universal on all Unix based systems but if it works on your systems. Instead I would avoid grep altogether:
ps aux | awk '/daemon_name/ && !/awk/ { print $2 }'
No need to escape anything in single quotation marks. ps aux
will give you the full list of processes on most Unix based systems and awk
is typically installed by default.
add a comment |
up vote
3
down vote
Use pgrep
to look for the pid of a process by name:
pgrep proc_name
With extra process name in the result (-l
):
pgrep -l proc_name
Look for and display the process name (-l
) and arguments (-f
):
pgrep -lf proc_name_or_argument
The good thing about pgrep
is that it will never report itself as a match. But you don't need to get the pid by pgrep
and then kill the corresponding process by kill
. Use pkill
instead:
pkill proc_name
Specify the SIGKILL
signal (-9
or -KILL
) instead of SIGTERM
(by default):
pkill -9 proc_name
Look for the process name (-l
) and arguments (-f
), ask for confirmation (-I
) before killing it by SIGKILL
signal (-9
or -KILL
):
pkill -KILL -Ilf proc_name_or_argument
Notice that the -I
option is only available on some versions of pkill
, e.g. the one on the OS X Mavericks.
add a comment |
up vote
0
down vote
If you are using bash, you can also do this in the following manner by making use of ps -eaf
PIDS=$(ps -eaf)
PID=$(echo "$PIDS" | grep "process_name" | awk '{print $2}')
echo $PID
This Is needlessly complex. Why not simply callps -eaf | grep "process_name" | awk '{print $2}'
– David Nedrow
Jul 4 at 13:25
add a comment |
up vote
0
down vote
this line gives you back the pid (process id) excluding "grep"
PID=$(ps aux | grep '/usr/bin/python2.7 manage.py SES__boto3_sqs_read' | grep -v grep)
Some explanation would be nice.
– davidbaumann
Nov 19 at 15:03
This is basically the same as the accepted answer, but with different decoration.
– Scott
Nov 19 at 20:29
add a comment |
7 Answers
7
active
oldest
votes
7 Answers
7
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
26
down vote
accepted
grep's -v
switch reverses the result, excluding it from the queue. So make it like:
ps aux | grep daemon_name | grep -v "grep daemon_name" | awk "{ print $2 }"
Upd. You can also use -C
switch to specify command name like so:
ps -C daemon_name -o pid=
The latter -o
determines which columns of the information you want in the listing. pid
lists only the process id column. And the equal sign =
after pid
means there will be no column title for that one, so you get only the clear numbers - PID's.
Hope this helps.
2
Using "grep -v grep": Just watch out that you may have a long command line including "grep" (Let's say somebody wrote a grep library=> "-Llibgrep.so" or "-cp libgrep.jar". At least I would repeat the pattern : grep -v "grep daemon_name"
– phil_w
Apr 9 '15 at 15:01
Good point, @phil_w, thanks. Updated the answer.
– Serg ikS
Apr 10 '15 at 7:50
add a comment |
up vote
26
down vote
accepted
grep's -v
switch reverses the result, excluding it from the queue. So make it like:
ps aux | grep daemon_name | grep -v "grep daemon_name" | awk "{ print $2 }"
Upd. You can also use -C
switch to specify command name like so:
ps -C daemon_name -o pid=
The latter -o
determines which columns of the information you want in the listing. pid
lists only the process id column. And the equal sign =
after pid
means there will be no column title for that one, so you get only the clear numbers - PID's.
Hope this helps.
2
Using "grep -v grep": Just watch out that you may have a long command line including "grep" (Let's say somebody wrote a grep library=> "-Llibgrep.so" or "-cp libgrep.jar". At least I would repeat the pattern : grep -v "grep daemon_name"
– phil_w
Apr 9 '15 at 15:01
Good point, @phil_w, thanks. Updated the answer.
– Serg ikS
Apr 10 '15 at 7:50
add a comment |
up vote
26
down vote
accepted
up vote
26
down vote
accepted
grep's -v
switch reverses the result, excluding it from the queue. So make it like:
ps aux | grep daemon_name | grep -v "grep daemon_name" | awk "{ print $2 }"
Upd. You can also use -C
switch to specify command name like so:
ps -C daemon_name -o pid=
The latter -o
determines which columns of the information you want in the listing. pid
lists only the process id column. And the equal sign =
after pid
means there will be no column title for that one, so you get only the clear numbers - PID's.
Hope this helps.
grep's -v
switch reverses the result, excluding it from the queue. So make it like:
ps aux | grep daemon_name | grep -v "grep daemon_name" | awk "{ print $2 }"
Upd. You can also use -C
switch to specify command name like so:
ps -C daemon_name -o pid=
The latter -o
determines which columns of the information you want in the listing. pid
lists only the process id column. And the equal sign =
after pid
means there will be no column title for that one, so you get only the clear numbers - PID's.
Hope this helps.
edited Apr 10 '15 at 7:52
answered Apr 7 '12 at 12:19
Serg ikS
37635
37635
2
Using "grep -v grep": Just watch out that you may have a long command line including "grep" (Let's say somebody wrote a grep library=> "-Llibgrep.so" or "-cp libgrep.jar". At least I would repeat the pattern : grep -v "grep daemon_name"
– phil_w
Apr 9 '15 at 15:01
Good point, @phil_w, thanks. Updated the answer.
– Serg ikS
Apr 10 '15 at 7:50
add a comment |
2
Using "grep -v grep": Just watch out that you may have a long command line including "grep" (Let's say somebody wrote a grep library=> "-Llibgrep.so" or "-cp libgrep.jar". At least I would repeat the pattern : grep -v "grep daemon_name"
– phil_w
Apr 9 '15 at 15:01
Good point, @phil_w, thanks. Updated the answer.
– Serg ikS
Apr 10 '15 at 7:50
2
2
Using "grep -v grep": Just watch out that you may have a long command line including "grep" (Let's say somebody wrote a grep library=> "-Llibgrep.so" or "-cp libgrep.jar". At least I would repeat the pattern : grep -v "grep daemon_name"
– phil_w
Apr 9 '15 at 15:01
Using "grep -v grep": Just watch out that you may have a long command line including "grep" (Let's say somebody wrote a grep library=> "-Llibgrep.so" or "-cp libgrep.jar". At least I would repeat the pattern : grep -v "grep daemon_name"
– phil_w
Apr 9 '15 at 15:01
Good point, @phil_w, thanks. Updated the answer.
– Serg ikS
Apr 10 '15 at 7:50
Good point, @phil_w, thanks. Updated the answer.
– Serg ikS
Apr 10 '15 at 7:50
add a comment |
up vote
23
down vote
You can use a character class trick. "[d]" does not match "[d]" only "d".
ps aux | grep [d]aemon_name | awk "{ print $2 }"
I prefer this to using | grep -v grep
.
Heh that's a clever trick, I like it. I'm keeping things simple at the moment though while I'm learning, and| grep -v grep
will be easier to understand when I revisit my code.
– tak
Apr 7 '12 at 12:29
I am trying to understand how this [h]ack works. Could you please explain what you mean with"[d]" does not match "[d]" only "d"
?
– Nowaker
Sep 28 '13 at 21:13
@Damian: The square brackets are metacharacters (delimiting a set of characters) in the search expression so thegrep
command won't match theps
output line for thegrep
command itself (what tak called "a second PID" in the original question). expressiona[xy]d
does not match texta[xy]d
but it does match textaxd
or textayd
. Hence expression[f]oo
matches textfoo
but not text[f]oo
. I hope that is clear.
– RedGrittyBrick
Sep 28 '13 at 21:34
Okay, I was wrong about ARGV. This is just a regexp. Thanks.
– Nowaker
Sep 29 '13 at 1:29
Devilishly clever! Had to think about it for a moment -- thanks!
– Subfuzion
Oct 23 '13 at 18:46
|
show 4 more comments
up vote
23
down vote
You can use a character class trick. "[d]" does not match "[d]" only "d".
ps aux | grep [d]aemon_name | awk "{ print $2 }"
I prefer this to using | grep -v grep
.
Heh that's a clever trick, I like it. I'm keeping things simple at the moment though while I'm learning, and| grep -v grep
will be easier to understand when I revisit my code.
– tak
Apr 7 '12 at 12:29
I am trying to understand how this [h]ack works. Could you please explain what you mean with"[d]" does not match "[d]" only "d"
?
– Nowaker
Sep 28 '13 at 21:13
@Damian: The square brackets are metacharacters (delimiting a set of characters) in the search expression so thegrep
command won't match theps
output line for thegrep
command itself (what tak called "a second PID" in the original question). expressiona[xy]d
does not match texta[xy]d
but it does match textaxd
or textayd
. Hence expression[f]oo
matches textfoo
but not text[f]oo
. I hope that is clear.
– RedGrittyBrick
Sep 28 '13 at 21:34
Okay, I was wrong about ARGV. This is just a regexp. Thanks.
– Nowaker
Sep 29 '13 at 1:29
Devilishly clever! Had to think about it for a moment -- thanks!
– Subfuzion
Oct 23 '13 at 18:46
|
show 4 more comments
up vote
23
down vote
up vote
23
down vote
You can use a character class trick. "[d]" does not match "[d]" only "d".
ps aux | grep [d]aemon_name | awk "{ print $2 }"
I prefer this to using | grep -v grep
.
You can use a character class trick. "[d]" does not match "[d]" only "d".
ps aux | grep [d]aemon_name | awk "{ print $2 }"
I prefer this to using | grep -v grep
.
answered Apr 7 '12 at 12:26
RedGrittyBrick
66.3k12104160
66.3k12104160
Heh that's a clever trick, I like it. I'm keeping things simple at the moment though while I'm learning, and| grep -v grep
will be easier to understand when I revisit my code.
– tak
Apr 7 '12 at 12:29
I am trying to understand how this [h]ack works. Could you please explain what you mean with"[d]" does not match "[d]" only "d"
?
– Nowaker
Sep 28 '13 at 21:13
@Damian: The square brackets are metacharacters (delimiting a set of characters) in the search expression so thegrep
command won't match theps
output line for thegrep
command itself (what tak called "a second PID" in the original question). expressiona[xy]d
does not match texta[xy]d
but it does match textaxd
or textayd
. Hence expression[f]oo
matches textfoo
but not text[f]oo
. I hope that is clear.
– RedGrittyBrick
Sep 28 '13 at 21:34
Okay, I was wrong about ARGV. This is just a regexp. Thanks.
– Nowaker
Sep 29 '13 at 1:29
Devilishly clever! Had to think about it for a moment -- thanks!
– Subfuzion
Oct 23 '13 at 18:46
|
show 4 more comments
Heh that's a clever trick, I like it. I'm keeping things simple at the moment though while I'm learning, and| grep -v grep
will be easier to understand when I revisit my code.
– tak
Apr 7 '12 at 12:29
I am trying to understand how this [h]ack works. Could you please explain what you mean with"[d]" does not match "[d]" only "d"
?
– Nowaker
Sep 28 '13 at 21:13
@Damian: The square brackets are metacharacters (delimiting a set of characters) in the search expression so thegrep
command won't match theps
output line for thegrep
command itself (what tak called "a second PID" in the original question). expressiona[xy]d
does not match texta[xy]d
but it does match textaxd
or textayd
. Hence expression[f]oo
matches textfoo
but not text[f]oo
. I hope that is clear.
– RedGrittyBrick
Sep 28 '13 at 21:34
Okay, I was wrong about ARGV. This is just a regexp. Thanks.
– Nowaker
Sep 29 '13 at 1:29
Devilishly clever! Had to think about it for a moment -- thanks!
– Subfuzion
Oct 23 '13 at 18:46
Heh that's a clever trick, I like it. I'm keeping things simple at the moment though while I'm learning, and
| grep -v grep
will be easier to understand when I revisit my code.– tak
Apr 7 '12 at 12:29
Heh that's a clever trick, I like it. I'm keeping things simple at the moment though while I'm learning, and
| grep -v grep
will be easier to understand when I revisit my code.– tak
Apr 7 '12 at 12:29
I am trying to understand how this [h]ack works. Could you please explain what you mean with
"[d]" does not match "[d]" only "d"
?– Nowaker
Sep 28 '13 at 21:13
I am trying to understand how this [h]ack works. Could you please explain what you mean with
"[d]" does not match "[d]" only "d"
?– Nowaker
Sep 28 '13 at 21:13
@Damian: The square brackets are metacharacters (delimiting a set of characters) in the search expression so the
grep
command won't match the ps
output line for the grep
command itself (what tak called "a second PID" in the original question). expression a[xy]d
does not match text a[xy]d
but it does match text axd
or text ayd
. Hence expression [f]oo
matches text foo
but not text [f]oo
. I hope that is clear.– RedGrittyBrick
Sep 28 '13 at 21:34
@Damian: The square brackets are metacharacters (delimiting a set of characters) in the search expression so the
grep
command won't match the ps
output line for the grep
command itself (what tak called "a second PID" in the original question). expression a[xy]d
does not match text a[xy]d
but it does match text axd
or text ayd
. Hence expression [f]oo
matches text foo
but not text [f]oo
. I hope that is clear.– RedGrittyBrick
Sep 28 '13 at 21:34
Okay, I was wrong about ARGV. This is just a regexp. Thanks.
– Nowaker
Sep 29 '13 at 1:29
Okay, I was wrong about ARGV. This is just a regexp. Thanks.
– Nowaker
Sep 29 '13 at 1:29
Devilishly clever! Had to think about it for a moment -- thanks!
– Subfuzion
Oct 23 '13 at 18:46
Devilishly clever! Had to think about it for a moment -- thanks!
– Subfuzion
Oct 23 '13 at 18:46
|
show 4 more comments
up vote
15
down vote
Avoid parsing ps
's output if there are more reliable alternatives.
pgrep daemon_name
pidof daemon_name
Yay, finally process management makes sense now.
– Błażej Michalik
Jun 2 '16 at 6:09
pgrep
searches by substring whilepidof
by exact match - exaclty what I needed! thanks!
– Sasha
Aug 19 '16 at 11:14
add a comment |
up vote
15
down vote
Avoid parsing ps
's output if there are more reliable alternatives.
pgrep daemon_name
pidof daemon_name
Yay, finally process management makes sense now.
– Błażej Michalik
Jun 2 '16 at 6:09
pgrep
searches by substring whilepidof
by exact match - exaclty what I needed! thanks!
– Sasha
Aug 19 '16 at 11:14
add a comment |
up vote
15
down vote
up vote
15
down vote
Avoid parsing ps
's output if there are more reliable alternatives.
pgrep daemon_name
pidof daemon_name
Avoid parsing ps
's output if there are more reliable alternatives.
pgrep daemon_name
pidof daemon_name
answered Apr 7 '12 at 12:29
grawity
229k35481540
229k35481540
Yay, finally process management makes sense now.
– Błażej Michalik
Jun 2 '16 at 6:09
pgrep
searches by substring whilepidof
by exact match - exaclty what I needed! thanks!
– Sasha
Aug 19 '16 at 11:14
add a comment |
Yay, finally process management makes sense now.
– Błażej Michalik
Jun 2 '16 at 6:09
pgrep
searches by substring whilepidof
by exact match - exaclty what I needed! thanks!
– Sasha
Aug 19 '16 at 11:14
Yay, finally process management makes sense now.
– Błażej Michalik
Jun 2 '16 at 6:09
Yay, finally process management makes sense now.
– Błażej Michalik
Jun 2 '16 at 6:09
pgrep
searches by substring while pidof
by exact match - exaclty what I needed! thanks!– Sasha
Aug 19 '16 at 11:14
pgrep
searches by substring while pidof
by exact match - exaclty what I needed! thanks!– Sasha
Aug 19 '16 at 11:14
add a comment |
up vote
3
down vote
The ps -C
option is not universal on all Unix based systems but if it works on your systems. Instead I would avoid grep altogether:
ps aux | awk '/daemon_name/ && !/awk/ { print $2 }'
No need to escape anything in single quotation marks. ps aux
will give you the full list of processes on most Unix based systems and awk
is typically installed by default.
add a comment |
up vote
3
down vote
The ps -C
option is not universal on all Unix based systems but if it works on your systems. Instead I would avoid grep altogether:
ps aux | awk '/daemon_name/ && !/awk/ { print $2 }'
No need to escape anything in single quotation marks. ps aux
will give you the full list of processes on most Unix based systems and awk
is typically installed by default.
add a comment |
up vote
3
down vote
up vote
3
down vote
The ps -C
option is not universal on all Unix based systems but if it works on your systems. Instead I would avoid grep altogether:
ps aux | awk '/daemon_name/ && !/awk/ { print $2 }'
No need to escape anything in single quotation marks. ps aux
will give you the full list of processes on most Unix based systems and awk
is typically installed by default.
The ps -C
option is not universal on all Unix based systems but if it works on your systems. Instead I would avoid grep altogether:
ps aux | awk '/daemon_name/ && !/awk/ { print $2 }'
No need to escape anything in single quotation marks. ps aux
will give you the full list of processes on most Unix based systems and awk
is typically installed by default.
answered Apr 29 '13 at 2:52
MOG73
311
311
add a comment |
add a comment |
up vote
3
down vote
Use pgrep
to look for the pid of a process by name:
pgrep proc_name
With extra process name in the result (-l
):
pgrep -l proc_name
Look for and display the process name (-l
) and arguments (-f
):
pgrep -lf proc_name_or_argument
The good thing about pgrep
is that it will never report itself as a match. But you don't need to get the pid by pgrep
and then kill the corresponding process by kill
. Use pkill
instead:
pkill proc_name
Specify the SIGKILL
signal (-9
or -KILL
) instead of SIGTERM
(by default):
pkill -9 proc_name
Look for the process name (-l
) and arguments (-f
), ask for confirmation (-I
) before killing it by SIGKILL
signal (-9
or -KILL
):
pkill -KILL -Ilf proc_name_or_argument
Notice that the -I
option is only available on some versions of pkill
, e.g. the one on the OS X Mavericks.
add a comment |
up vote
3
down vote
Use pgrep
to look for the pid of a process by name:
pgrep proc_name
With extra process name in the result (-l
):
pgrep -l proc_name
Look for and display the process name (-l
) and arguments (-f
):
pgrep -lf proc_name_or_argument
The good thing about pgrep
is that it will never report itself as a match. But you don't need to get the pid by pgrep
and then kill the corresponding process by kill
. Use pkill
instead:
pkill proc_name
Specify the SIGKILL
signal (-9
or -KILL
) instead of SIGTERM
(by default):
pkill -9 proc_name
Look for the process name (-l
) and arguments (-f
), ask for confirmation (-I
) before killing it by SIGKILL
signal (-9
or -KILL
):
pkill -KILL -Ilf proc_name_or_argument
Notice that the -I
option is only available on some versions of pkill
, e.g. the one on the OS X Mavericks.
add a comment |
up vote
3
down vote
up vote
3
down vote
Use pgrep
to look for the pid of a process by name:
pgrep proc_name
With extra process name in the result (-l
):
pgrep -l proc_name
Look for and display the process name (-l
) and arguments (-f
):
pgrep -lf proc_name_or_argument
The good thing about pgrep
is that it will never report itself as a match. But you don't need to get the pid by pgrep
and then kill the corresponding process by kill
. Use pkill
instead:
pkill proc_name
Specify the SIGKILL
signal (-9
or -KILL
) instead of SIGTERM
(by default):
pkill -9 proc_name
Look for the process name (-l
) and arguments (-f
), ask for confirmation (-I
) before killing it by SIGKILL
signal (-9
or -KILL
):
pkill -KILL -Ilf proc_name_or_argument
Notice that the -I
option is only available on some versions of pkill
, e.g. the one on the OS X Mavericks.
Use pgrep
to look for the pid of a process by name:
pgrep proc_name
With extra process name in the result (-l
):
pgrep -l proc_name
Look for and display the process name (-l
) and arguments (-f
):
pgrep -lf proc_name_or_argument
The good thing about pgrep
is that it will never report itself as a match. But you don't need to get the pid by pgrep
and then kill the corresponding process by kill
. Use pkill
instead:
pkill proc_name
Specify the SIGKILL
signal (-9
or -KILL
) instead of SIGTERM
(by default):
pkill -9 proc_name
Look for the process name (-l
) and arguments (-f
), ask for confirmation (-I
) before killing it by SIGKILL
signal (-9
or -KILL
):
pkill -KILL -Ilf proc_name_or_argument
Notice that the -I
option is only available on some versions of pkill
, e.g. the one on the OS X Mavericks.
edited Aug 26 '14 at 1:28
answered Aug 26 '14 at 1:21
Rockallite
21123
21123
add a comment |
add a comment |
up vote
0
down vote
If you are using bash, you can also do this in the following manner by making use of ps -eaf
PIDS=$(ps -eaf)
PID=$(echo "$PIDS" | grep "process_name" | awk '{print $2}')
echo $PID
This Is needlessly complex. Why not simply callps -eaf | grep "process_name" | awk '{print $2}'
– David Nedrow
Jul 4 at 13:25
add a comment |
up vote
0
down vote
If you are using bash, you can also do this in the following manner by making use of ps -eaf
PIDS=$(ps -eaf)
PID=$(echo "$PIDS" | grep "process_name" | awk '{print $2}')
echo $PID
This Is needlessly complex. Why not simply callps -eaf | grep "process_name" | awk '{print $2}'
– David Nedrow
Jul 4 at 13:25
add a comment |
up vote
0
down vote
up vote
0
down vote
If you are using bash, you can also do this in the following manner by making use of ps -eaf
PIDS=$(ps -eaf)
PID=$(echo "$PIDS" | grep "process_name" | awk '{print $2}')
echo $PID
If you are using bash, you can also do this in the following manner by making use of ps -eaf
PIDS=$(ps -eaf)
PID=$(echo "$PIDS" | grep "process_name" | awk '{print $2}')
echo $PID
answered Mar 21 '17 at 9:19
Shubham Khatri
1012
1012
This Is needlessly complex. Why not simply callps -eaf | grep "process_name" | awk '{print $2}'
– David Nedrow
Jul 4 at 13:25
add a comment |
This Is needlessly complex. Why not simply callps -eaf | grep "process_name" | awk '{print $2}'
– David Nedrow
Jul 4 at 13:25
This Is needlessly complex. Why not simply call
ps -eaf | grep "process_name" | awk '{print $2}'
– David Nedrow
Jul 4 at 13:25
This Is needlessly complex. Why not simply call
ps -eaf | grep "process_name" | awk '{print $2}'
– David Nedrow
Jul 4 at 13:25
add a comment |
up vote
0
down vote
this line gives you back the pid (process id) excluding "grep"
PID=$(ps aux | grep '/usr/bin/python2.7 manage.py SES__boto3_sqs_read' | grep -v grep)
Some explanation would be nice.
– davidbaumann
Nov 19 at 15:03
This is basically the same as the accepted answer, but with different decoration.
– Scott
Nov 19 at 20:29
add a comment |
up vote
0
down vote
this line gives you back the pid (process id) excluding "grep"
PID=$(ps aux | grep '/usr/bin/python2.7 manage.py SES__boto3_sqs_read' | grep -v grep)
Some explanation would be nice.
– davidbaumann
Nov 19 at 15:03
This is basically the same as the accepted answer, but with different decoration.
– Scott
Nov 19 at 20:29
add a comment |
up vote
0
down vote
up vote
0
down vote
this line gives you back the pid (process id) excluding "grep"
PID=$(ps aux | grep '/usr/bin/python2.7 manage.py SES__boto3_sqs_read' | grep -v grep)
this line gives you back the pid (process id) excluding "grep"
PID=$(ps aux | grep '/usr/bin/python2.7 manage.py SES__boto3_sqs_read' | grep -v grep)
edited Nov 19 at 19:31
answered Nov 19 at 14:37
Brian Sanchez
1012
1012
Some explanation would be nice.
– davidbaumann
Nov 19 at 15:03
This is basically the same as the accepted answer, but with different decoration.
– Scott
Nov 19 at 20:29
add a comment |
Some explanation would be nice.
– davidbaumann
Nov 19 at 15:03
This is basically the same as the accepted answer, but with different decoration.
– Scott
Nov 19 at 20:29
Some explanation would be nice.
– davidbaumann
Nov 19 at 15:03
Some explanation would be nice.
– davidbaumann
Nov 19 at 15:03
This is basically the same as the accepted answer, but with different decoration.
– Scott
Nov 19 at 20:29
This is basically the same as the accepted answer, but with different decoration.
– Scott
Nov 19 at 20:29
add a comment |
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%2fsuperuser.com%2fquestions%2f409655%2fexcluding-grep-from-process-list%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
serverfault.com/questions/367921/…
– Der Hochstapler
Apr 7 '12 at 12:29