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.

  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?

migrated from stackoverflow.com May 20 '13 at 6:01

This question came from our site for professional and enthusiast programmers.


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'
    : already running
    : start it up

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.

  • Your statement doesn't work either, but now I think I understand the reason: The statement is supposed to fail if it doesn't find any matching lines which is a totally valid case. After the grep statements I have an if which checks the error code of the last statement, but because of an error code of 1 if the mathing line is not found and "sh -e" the if is never reached. There's nothing wrong with escape characters or else. – Thorsten Schöning May 16 '13 at 21:21
  • 1
    You should fix grep foo bar; if [ $? = 0 ]; then ... to the simpler, more robust, and more idiomatic if grep foo bar; then ... – tripleee May 16 '13 at 21:26
  • I couldn't get something like if [ ps ax | grep "postgres: wal writer process" | grep -v "grep" ] or if [ ps ax | grep "[p]ostgres: wal writer process" ] even without [] form the if and escaped | or [p] to work. – Thorsten Schöning May 16 '13 at 21:44
  • Maybe see also stackoverflow.com/questions/10586213/… – tripleee May 16 '13 at 21:52
  • 1
    Or, to wit, if ps aux | grep "[p]ostgres: wal writer process"; then ... and notice in particular the absence of square brackets around the commands. – tripleee May 16 '13 at 21:54

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.