Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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?

share|improve this question

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

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

1 Answer 1

up vote 1 down vote accepted

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 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.

share|improve this answer
    
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

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.