1

I'm using bash shell on Ubuntu Linux 14.04. I have this script ...

SIDEKIQ_PID="$APP_ROOT/tmp/sidekiq.pid"
SIDEKIQ_STOP_CMD="cd $APP_ROOT && bundle exec sidekiqctl stop $SIDEKIQ_PID 0"
...
    su - $USER -c "$SIDEKIQ_STOP_CMD"
    echo "Stopped sidekiq"

What I'm noticing is that if the PID no longer exists that is defined in my file "$SIDEKIQ_PID", the line

su - $USER -c "$SIDEKIQ_STOP_CMD"

dies with a "Process doesn't exist" error and the script doesn't continue (I can't see the following echo statement). How can I make my script continue even if the process no longer exists?

  • Should the stop subcommand take a PID file or a PID (number)? – Kusalananda Oct 10 '17 at 21:55
  • Everything works fine when the PID in taht file exists so I assume its good the way I have it, at least in regards to what comes after "stop". – Dave Oct 10 '17 at 22:00
  • First test whether the PID still exists with kill -0 $PID (this needs to be executed by the owner of the process or root). If the exit code is zero, the PID exists, and proceed; elsewise, handle the case of "this PID no longer exists". – DopeGhoti Oct 10 '17 at 22:03
  • 1
    Is errexit set for the shell invocation? – Jeff Schaller Oct 10 '17 at 23:29
  • 1
    any confirmation of the "errexit" shell option being set or unset? – Jeff Schaller Oct 13 '17 at 20:00
0
+50

A quick way to get around the errexit setting without turning it off is to make the command return success.

Example:

set -e
false  # script exits here

vs.

set -e
false || true   # script no longer exits

So maybe try

su - $USER -c "$SIDEKIQ_STOP_CMD" || true

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.