I have a script that uses killall
to make sure an app isn't already running before starting it:
#!/bin/bash
set -e
some_command
another_command
sudo killall myapp # this causes the script to fail if myapp isn't running
sleep 10
myapp start
However, if myapp isn't running, killall
will exit the script because it returns an error.
One option to work around this is to temporarily disable set -o errexit
:
#!/bin/bash
set -e
some_command
another_command
set +e
sudo killall myapp # this causes the script to fail if myapp isn't running
set -e
sleep 10
myapp start
However, the above approach is quite messy. What other options are there for temporarily disabling set -e
?
If you want just allow this single command to end with error and consider it normal you can:
sudo killall myapp || :
: is a noop in bash. Effectively the line above is as explicitly as possible expressing "on error do nothing". You can achieve the same effect with maybe a little more idiomatic:
sudo killall myapp || true
Of course you can also do something else like:
sudo killall myapp || echo "Failed to kill Myapp, probably it is not running at the moment." >&2
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With