pgrep returns different results if run from script than if run in terminal
Clash Royale CLAN TAG#URR8PPP
I have written a script to check how many instances of a process are running in an OpenWrt based system. If I run the following in my terminal
COUNT_PS=$(echo `ps -w | grep -v grep | grep upmpdcli | wc -l`)
root@SHAULA-720:~# echo $COUNT_PS
the result is
1
Below is the code of the shell script , if I run this script the result is 4 instead of 1
#!/bin/ash
#for debug
ps -w | grep -v grep | grep upmpdcli
COUNT_PS=$(echo `ps -w | grep -v grep | grep upmpdcli | wc -l`)
echo we have $COUNT_PS instances for upmpdcli;
logger we have $COUNT_PS instances for upmpdcli;
if [[ $COUNT_PS == 1 ]]; then
logger "we have only one instance"
#HERE PUT CODE TO START NEW PROCESS
elif [[ $COUNT_PS == 2 ]]; then
logger "we have 2 instances lets kill all and start a single"
kill -9 `pgrep upmpdcli`
elif [[ $COUNT_PS == 0 ]]; then
logger "we have no instance lets wait for cron to start it"
else
logger "we have $COUNT_PS instances"
fi
so if I run /etc/upmpd-check.sh
the result is we have 4 instances for upmpdcli
which is strange to me.
What am I missing here?
shell-script openwrt pgrep
|
show 7 more comments
I have written a script to check how many instances of a process are running in an OpenWrt based system. If I run the following in my terminal
COUNT_PS=$(echo `ps -w | grep -v grep | grep upmpdcli | wc -l`)
root@SHAULA-720:~# echo $COUNT_PS
the result is
1
Below is the code of the shell script , if I run this script the result is 4 instead of 1
#!/bin/ash
#for debug
ps -w | grep -v grep | grep upmpdcli
COUNT_PS=$(echo `ps -w | grep -v grep | grep upmpdcli | wc -l`)
echo we have $COUNT_PS instances for upmpdcli;
logger we have $COUNT_PS instances for upmpdcli;
if [[ $COUNT_PS == 1 ]]; then
logger "we have only one instance"
#HERE PUT CODE TO START NEW PROCESS
elif [[ $COUNT_PS == 2 ]]; then
logger "we have 2 instances lets kill all and start a single"
kill -9 `pgrep upmpdcli`
elif [[ $COUNT_PS == 0 ]]; then
logger "we have no instance lets wait for cron to start it"
else
logger "we have $COUNT_PS instances"
fi
so if I run /etc/upmpd-check.sh
the result is we have 4 instances for upmpdcli
which is strange to me.
What am I missing here?
shell-script openwrt pgrep
1
Why don't you usepgrep
instead ofps | grep
?
– Kusalananda
Jan 31 at 12:45
even with pgrep the results are same. if I runpgrep upmpdcli | wc -l
from terminal I would get proper result 1, but when I would run the script it will result 4
– dmSherazi
Jan 31 at 12:55
1
What is the output ofps -w | grep -v grep | grep upmpdcli
at the terminal and in the script? Are they showing the same set of processes, or just a snapshot of processes that rapidly get created and killed?
– Mark Plotnick
Jan 31 at 13:04
since you have pgrep, I want to make sure you're aware of pkill as well.
– Jeff Schaller
Jan 31 at 13:17
3
It's catching your shell script's name too. Why don't you just usepgrep
andpkill
with the appropriate expression and options?
– Kusalananda
Jan 31 at 13:38
|
show 7 more comments
I have written a script to check how many instances of a process are running in an OpenWrt based system. If I run the following in my terminal
COUNT_PS=$(echo `ps -w | grep -v grep | grep upmpdcli | wc -l`)
root@SHAULA-720:~# echo $COUNT_PS
the result is
1
Below is the code of the shell script , if I run this script the result is 4 instead of 1
#!/bin/ash
#for debug
ps -w | grep -v grep | grep upmpdcli
COUNT_PS=$(echo `ps -w | grep -v grep | grep upmpdcli | wc -l`)
echo we have $COUNT_PS instances for upmpdcli;
logger we have $COUNT_PS instances for upmpdcli;
if [[ $COUNT_PS == 1 ]]; then
logger "we have only one instance"
#HERE PUT CODE TO START NEW PROCESS
elif [[ $COUNT_PS == 2 ]]; then
logger "we have 2 instances lets kill all and start a single"
kill -9 `pgrep upmpdcli`
elif [[ $COUNT_PS == 0 ]]; then
logger "we have no instance lets wait for cron to start it"
else
logger "we have $COUNT_PS instances"
fi
so if I run /etc/upmpd-check.sh
the result is we have 4 instances for upmpdcli
which is strange to me.
What am I missing here?
shell-script openwrt pgrep
I have written a script to check how many instances of a process are running in an OpenWrt based system. If I run the following in my terminal
COUNT_PS=$(echo `ps -w | grep -v grep | grep upmpdcli | wc -l`)
root@SHAULA-720:~# echo $COUNT_PS
the result is
1
Below is the code of the shell script , if I run this script the result is 4 instead of 1
#!/bin/ash
#for debug
ps -w | grep -v grep | grep upmpdcli
COUNT_PS=$(echo `ps -w | grep -v grep | grep upmpdcli | wc -l`)
echo we have $COUNT_PS instances for upmpdcli;
logger we have $COUNT_PS instances for upmpdcli;
if [[ $COUNT_PS == 1 ]]; then
logger "we have only one instance"
#HERE PUT CODE TO START NEW PROCESS
elif [[ $COUNT_PS == 2 ]]; then
logger "we have 2 instances lets kill all and start a single"
kill -9 `pgrep upmpdcli`
elif [[ $COUNT_PS == 0 ]]; then
logger "we have no instance lets wait for cron to start it"
else
logger "we have $COUNT_PS instances"
fi
so if I run /etc/upmpd-check.sh
the result is we have 4 instances for upmpdcli
which is strange to me.
What am I missing here?
shell-script openwrt pgrep
shell-script openwrt pgrep
edited Jan 31 at 13:19
dmSherazi
asked Jan 31 at 12:43
dmSherazidmSherazi
1034
1034
1
Why don't you usepgrep
instead ofps | grep
?
– Kusalananda
Jan 31 at 12:45
even with pgrep the results are same. if I runpgrep upmpdcli | wc -l
from terminal I would get proper result 1, but when I would run the script it will result 4
– dmSherazi
Jan 31 at 12:55
1
What is the output ofps -w | grep -v grep | grep upmpdcli
at the terminal and in the script? Are they showing the same set of processes, or just a snapshot of processes that rapidly get created and killed?
– Mark Plotnick
Jan 31 at 13:04
since you have pgrep, I want to make sure you're aware of pkill as well.
– Jeff Schaller
Jan 31 at 13:17
3
It's catching your shell script's name too. Why don't you just usepgrep
andpkill
with the appropriate expression and options?
– Kusalananda
Jan 31 at 13:38
|
show 7 more comments
1
Why don't you usepgrep
instead ofps | grep
?
– Kusalananda
Jan 31 at 12:45
even with pgrep the results are same. if I runpgrep upmpdcli | wc -l
from terminal I would get proper result 1, but when I would run the script it will result 4
– dmSherazi
Jan 31 at 12:55
1
What is the output ofps -w | grep -v grep | grep upmpdcli
at the terminal and in the script? Are they showing the same set of processes, or just a snapshot of processes that rapidly get created and killed?
– Mark Plotnick
Jan 31 at 13:04
since you have pgrep, I want to make sure you're aware of pkill as well.
– Jeff Schaller
Jan 31 at 13:17
3
It's catching your shell script's name too. Why don't you just usepgrep
andpkill
with the appropriate expression and options?
– Kusalananda
Jan 31 at 13:38
1
1
Why don't you use
pgrep
instead of ps | grep
?– Kusalananda
Jan 31 at 12:45
Why don't you use
pgrep
instead of ps | grep
?– Kusalananda
Jan 31 at 12:45
even with pgrep the results are same. if I run
pgrep upmpdcli | wc -l
from terminal I would get proper result 1, but when I would run the script it will result 4– dmSherazi
Jan 31 at 12:55
even with pgrep the results are same. if I run
pgrep upmpdcli | wc -l
from terminal I would get proper result 1, but when I would run the script it will result 4– dmSherazi
Jan 31 at 12:55
1
1
What is the output of
ps -w | grep -v grep | grep upmpdcli
at the terminal and in the script? Are they showing the same set of processes, or just a snapshot of processes that rapidly get created and killed?– Mark Plotnick
Jan 31 at 13:04
What is the output of
ps -w | grep -v grep | grep upmpdcli
at the terminal and in the script? Are they showing the same set of processes, or just a snapshot of processes that rapidly get created and killed?– Mark Plotnick
Jan 31 at 13:04
since you have pgrep, I want to make sure you're aware of pkill as well.
– Jeff Schaller
Jan 31 at 13:17
since you have pgrep, I want to make sure you're aware of pkill as well.
– Jeff Schaller
Jan 31 at 13:17
3
3
It's catching your shell script's name too. Why don't you just use
pgrep
and pkill
with the appropriate expression and options?– Kusalananda
Jan 31 at 13:38
It's catching your shell script's name too. Why don't you just use
pgrep
and pkill
with the appropriate expression and options?– Kusalananda
Jan 31 at 13:38
|
show 7 more comments
1 Answer
1
active
oldest
votes
The main confusion comes from your ps | grep
pipeline matching the name of your script, which includes the string upmpdcli
.
With pgrep
you would not have the same issue as pgrep
will look at the command names only by default and would not mistake upmpdcli-check
for upmpdcli
.
Ideally, you would use
pgrep -x /usr/bin/upmpdcli
to get the PIDs for that process.
To kill that process, or those processes, use
pkill -x /usr/bin/upmpdcli
That is, do not use the PIDs had from pgrep
(as these may not be up to date).
To kill only the oldest upmpdcli
process, use pkill
with -o
, and use -n
to kill only the newest. See the pkill
manual.
Also note that
variable=$( echo `somecommand` )
is better written as
variable=$( some_command )
unless you are relying on the fact that the shell will do word splitting and filename expansions on the result of some_command
(you are not).
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%2f497927%2fpgrep-returns-different-results-if-run-from-script-than-if-run-in-terminal%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
The main confusion comes from your ps | grep
pipeline matching the name of your script, which includes the string upmpdcli
.
With pgrep
you would not have the same issue as pgrep
will look at the command names only by default and would not mistake upmpdcli-check
for upmpdcli
.
Ideally, you would use
pgrep -x /usr/bin/upmpdcli
to get the PIDs for that process.
To kill that process, or those processes, use
pkill -x /usr/bin/upmpdcli
That is, do not use the PIDs had from pgrep
(as these may not be up to date).
To kill only the oldest upmpdcli
process, use pkill
with -o
, and use -n
to kill only the newest. See the pkill
manual.
Also note that
variable=$( echo `somecommand` )
is better written as
variable=$( some_command )
unless you are relying on the fact that the shell will do word splitting and filename expansions on the result of some_command
(you are not).
add a comment |
The main confusion comes from your ps | grep
pipeline matching the name of your script, which includes the string upmpdcli
.
With pgrep
you would not have the same issue as pgrep
will look at the command names only by default and would not mistake upmpdcli-check
for upmpdcli
.
Ideally, you would use
pgrep -x /usr/bin/upmpdcli
to get the PIDs for that process.
To kill that process, or those processes, use
pkill -x /usr/bin/upmpdcli
That is, do not use the PIDs had from pgrep
(as these may not be up to date).
To kill only the oldest upmpdcli
process, use pkill
with -o
, and use -n
to kill only the newest. See the pkill
manual.
Also note that
variable=$( echo `somecommand` )
is better written as
variable=$( some_command )
unless you are relying on the fact that the shell will do word splitting and filename expansions on the result of some_command
(you are not).
add a comment |
The main confusion comes from your ps | grep
pipeline matching the name of your script, which includes the string upmpdcli
.
With pgrep
you would not have the same issue as pgrep
will look at the command names only by default and would not mistake upmpdcli-check
for upmpdcli
.
Ideally, you would use
pgrep -x /usr/bin/upmpdcli
to get the PIDs for that process.
To kill that process, or those processes, use
pkill -x /usr/bin/upmpdcli
That is, do not use the PIDs had from pgrep
(as these may not be up to date).
To kill only the oldest upmpdcli
process, use pkill
with -o
, and use -n
to kill only the newest. See the pkill
manual.
Also note that
variable=$( echo `somecommand` )
is better written as
variable=$( some_command )
unless you are relying on the fact that the shell will do word splitting and filename expansions on the result of some_command
(you are not).
The main confusion comes from your ps | grep
pipeline matching the name of your script, which includes the string upmpdcli
.
With pgrep
you would not have the same issue as pgrep
will look at the command names only by default and would not mistake upmpdcli-check
for upmpdcli
.
Ideally, you would use
pgrep -x /usr/bin/upmpdcli
to get the PIDs for that process.
To kill that process, or those processes, use
pkill -x /usr/bin/upmpdcli
That is, do not use the PIDs had from pgrep
(as these may not be up to date).
To kill only the oldest upmpdcli
process, use pkill
with -o
, and use -n
to kill only the newest. See the pkill
manual.
Also note that
variable=$( echo `somecommand` )
is better written as
variable=$( some_command )
unless you are relying on the fact that the shell will do word splitting and filename expansions on the result of some_command
(you are not).
edited Feb 1 at 13:00
answered Feb 1 at 12:36
KusalanandaKusalananda
131k17250409
131k17250409
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%2f497927%2fpgrep-returns-different-results-if-run-from-script-than-if-run-in-terminal%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
1
Why don't you use
pgrep
instead ofps | grep
?– Kusalananda
Jan 31 at 12:45
even with pgrep the results are same. if I run
pgrep upmpdcli | wc -l
from terminal I would get proper result 1, but when I would run the script it will result 4– dmSherazi
Jan 31 at 12:55
1
What is the output of
ps -w | grep -v grep | grep upmpdcli
at the terminal and in the script? Are they showing the same set of processes, or just a snapshot of processes that rapidly get created and killed?– Mark Plotnick
Jan 31 at 13:04
since you have pgrep, I want to make sure you're aware of pkill as well.
– Jeff Schaller
Jan 31 at 13:17
3
It's catching your shell script's name too. Why don't you just use
pgrep
andpkill
with the appropriate expression and options?– Kusalananda
Jan 31 at 13:38