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 here enter image description here

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.