Dave M
Dave M

Reputation: 73

Bash script not exiting once a process is running

How should I modify my bash script logic so it exits the while loop and exits the script itself once a process named custom_app is running on my local Ubuntu 18.04? I've tried using break and exit inside an if statement with no luck.

Once custom app is running from say...1st attempt then I quit the app, run_custom_app.sh lingers in the background and resumes retrying 2nd, 3rd, 4th, 5th time. It should be doing nothing at this point since app already ran successfully and user intentionally quit.

Below is run_custom_app.sh used to run my custom app triggered from a website button click.

Script logic

  1. Check if custom_app process is running already. If so, don't run the commands in the while code block. Do nothing. Exit run_custom_app.sh.
  2. While custom_app process is NOT running, retry up to 5 times.

  3. Once custom_app process is running, stop while loop and exit run_custom_app.sh as well.

  4. In cases where 5 run retries have been attempted but custom_app process is still not running, display a message to the user.
#!/bin/sh

RETRYCOUNT=0
PROCESS_RUNNING=`ps cax | grep custom_app`

# Try to connect until process is running. Retry up to 5 times. Wait 10 secs between each retry.

while [ ! "$PROCESS_RUNNING" ] && [ "$RETRYCOUNT" -le 5 ]; do
  RETRYCOUNT="`expr $RETRYCOUNT + 1`"

  commands

  sleep 10

  PROCESS_RUNNING=`ps cax | grep custom_app`

  if [ "$PROCESS_RUNNING" ]; then
    break
  fi
done


# Display an error message if not connected after 5 connection attempts
if [ ! "$PROCESS_RUNNING" ]; then
  echo "Failed to connect, please try again in about 2 minutes"   # I need to modify this later so it opens a Terminal window displaying the echo statement, not yet sure how.
fi

Upvotes: 0

Views: 806

Answers (1)

Jetchisel
Jetchisel

Reputation: 7791

I have tested this code on VirtualBox as a replacement for your custom_app and the previous post was using an until loop and pgrep instead of ps. As suggested by DavidC.Rankin pidof is more correct but if you want to use ps then I suggest to use ps -C custom_app -o pid=

#!/bin/sh

retrycount=0

until my_app_pid=$(ps -C VirtualBox -o pid=); do  ##: save the output of ps in a variable so we can check/test it for later.
  echo commands  ##: Just echoed the command here not sure which commands you are using/running.
  if [ "$retrycount" -eq 4 ]; then ##: We started at 0 so the fifth count is 4
    break  ##: exit the loop 
  fi
  sleep 10
  retrycount=$((retrycount+1))  ##: increment by one using shell syntax without expr
done

if [ -n "$my_app_pid" ]; then  ##: if $my_app_pid is not empty
  echo "app is running"
else
  echo "Failed to connect, please try again in about 2 minutes"  >&2 ##: print the message to stderr 
  exit 1 ##: exit with a failure which is not 0
fi
  • The my_app_pid=$(ps -C VirtualBox -o pid=) variable assignment has a useful exit status so we can use it.

  • Basically the until loop is just the opposite of the while loop.

Upvotes: 2

Related Questions