1

I am working with some embedded Linux distribution built with Yocto. I need to check in a sh script whether some app is running. When the app is running if statement is executed, but if app is not running the else statement is not executed and nothing is shown on a screen. I tested this piece of code on a PC and it works properly. Do you have any idea why this code doesn't work on my embedded Linux distribution? The only difference I can see is that on my embedded Linux echo $SHELL shows /bin/sh and on PC it shows /bin/bash but it is very simple code and it should works both with bash and sh.

My code:

#!/bin/sh

check_status()
{
    pid=`pidof -x my_app`
    if [ -n "$pid" ]; then
        echo "My_app is running."
    else
        echo "My_app is stopped."
    fi  
}   

check_status

Thank you very much in advance for any help.

EDIT:

  1. pidof is available on my embedded Linux.
  2. I set my shell as /bin/bash instead of /bin/sh and reboot the system.
  3. I replaced the first line of my script with the following one: #!/usr/bin/env bash.

Neither 2nd, nor 3rd solution solved my problem.

  • Replace the first line with `#!/usr/bin/env bash` and see if it works. – nino Jul 06 '21 at 13:30
  • 1
    Is `pidof` available? – KamilCuk Jul 06 '21 at 13:32
  • 1
    Can you please [edit] to include the output from `sh -x scriptname`? – tripleee Jul 06 '21 at 13:46
  • 1
    As an aside, checking whether the ouput is empty is basically a variant of [Why is testing ”$?” to see if a command succeeded or not, an anti-pattern?](https://stackoverflow.com/questions/36313216/why-is-testing-to-see-if-a-command-succeeded-or-not-an-anti-pattern) You simply want `if pidof -x my_app >/dev/null; then ...` – tripleee Jul 06 '21 at 13:56
  • @userr019283 - It seems you execute the script with the `-e` option set. – Armali Jul 06 '21 at 19:45
  • 1
    Add a simple `echo` statement between `pid=...` and the `if` statement to see if your `if` statement executes at all (or more precisely, to see if your function continues past the call to `pidof`). – chepner Jul 07 '21 at 03:08
  • 1
    Thank you all for your help. I have `set -e` in my script and when my app wasn't running `pidof -x my_app` returned 1 so the script was stopped at that point. – userr019283 Jul 07 '21 at 06:11

0 Answers0