Wednesday, 29 January 2020

shell - grep fails in upstart script


I have an upstart configuration for a service which needs to check the existance of a process on it's startup. This sounds fairly easy but I'm already trying for 3 hours to get this thing solved and I'm lost. The problem seems to be that some grep statements lead to an exit of the script within upstart, leading to a failure in starting the service. But upstart doesn't log anything, it just exits the script and is gone.


Following are some example lines which trigger the problem: The first echo and the output of it's grep are logged, the second echo is logged, but not the output of it's grep. After the second echo upstart just exits the script and stops. The problem is adding the -v option or something other like using regexp classes like [[:digit:]]. It seems like I need to escape some characters but I simply don't know how. A simple -v or something like that doesn't work.


script
echo grep
ps ax | grep "postgres: wal writer process" | grep "grep"
echo grep2
ps ax | grep "postgres: wal writer process" | grep -v "grep"
end script

Does anyone has an idea on what I'm doing wrong and how to fix the problem?



Answer



As explained in the manual, all scripts run with sh -e. That means any unhandled command failure will terminate the script with an error. If you have code which might return failure, you would code it like


command || true

or wrap it in a conditional or something.


By the by, your code is better written as


ps ax | grep "[p]ostgres: wal writer process"

See also https://stackoverflow.com/questions/9375711/more-elegant-ps-aux-grep-v-grep


So in summary, you would probably be looking for something like


if ps aux | grep '[p]ostgres: wal writer process'
then
: already running
else
: start it up
fi

Now that grep is run as part of a conditional, a failure is no longer considered unhandled (even if you don't have an else clause!) and so it will run safely under set -e.


No comments:

Post a Comment

How can I VLOOKUP in multiple Excel documents?

I am trying to VLOOKUP reference data with around 400 seperate Excel files. Is it possible to do this in a quick way rather than doing it m...