pgrep returns different results if run from script than if run in terminal

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP












0















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?



enter image description hereenter image description here










share|improve this question



















  • 1





    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






  • 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 and pkill with the appropriate expression and options?

    – Kusalananda
    Jan 31 at 13:38















0















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?



enter image description hereenter image description here










share|improve this question



















  • 1





    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






  • 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 and pkill with the appropriate expression and options?

    – Kusalananda
    Jan 31 at 13:38













0












0








0








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?



enter image description hereenter image description here










share|improve this question
















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?



enter image description hereenter image description here







shell-script openwrt pgrep






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 31 at 13:19







dmSherazi

















asked Jan 31 at 12:43









dmSherazidmSherazi

1034




1034







  • 1





    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






  • 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 and pkill with the appropriate expression and options?

    – Kusalananda
    Jan 31 at 13:38












  • 1





    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






  • 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 and pkill 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










1 Answer
1






active

oldest

votes


















3














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).






share|improve this answer
























    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
    );



    );













    draft saved

    draft discarded


















    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









    3














    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).






    share|improve this answer





























      3














      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).






      share|improve this answer



























        3












        3








        3







        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).






        share|improve this answer















        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).







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Feb 1 at 13:00

























        answered Feb 1 at 12:36









        KusalanandaKusalananda

        131k17250409




        131k17250409



























            draft saved

            draft discarded
















































            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.




            draft saved


            draft discarded














            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





















































            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






            Popular posts from this blog

            How to check contact read email or not when send email to Individual?

            Bahrain

            Postfix configuration issue with fips on centos 7; mailgun relay