Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I'm trying to trigger a reload of the Nginx master process by using pkill and sudo.

The server reloads fine, but I was just curious if anyone knows why the command sudo pkill -HUP -f "nginx: master process" returns code 129?

# Running as root
$ pkill -HUP -f "nginx: master process"
$ echo $?

# Output is as expected:
0

# Running as a user (sudo is set to not prompt for a password)
% sudo pkill -HUP -f "nginx: master process
% echo $?

# Output is weird (considering sudo should be passing along the return
#                  code of the command it is executing):
129

Running on Ubuntu 12.04.1 LTS running on EC2 ... surprisingly Ubuntu 12.04.1 LTS running on a physical computer (not installed using cloud images) runs fine.

share|improve this question

2 Answers 2

up vote 4 down vote accepted

Usually, when the shell returns a status code above 128, it means that the process was killed by a signal. Subtract 128 to get the signal number. Your kill command was killed by signal 1, which is HUP.

pkill takes care never to kill itself. But it matched its parent, the sudo process.

There are several ways to avoid this:

  • Use pkill -x to consider only exact matches and not substrings (recommended if possible, this is the best way to avoid spurious matches).
  • Don't use the -f option.
  • (Last resort) Write a pattern that doesn't match itself, e.g. "[n]ginx: master process".
share|improve this answer
    
Awesome idea ... I used pkill -x and just used the regex patter "^nginx: master process.*$" which worked like a charm! Thank you! End result: sudo pkill -HUP -x -f "^nginx: master process.*$" –  iserko Aug 17 '12 at 13:57

sudo returns 129 because pkill is sending the HUP signal to sudo. This is because sudo's command line matches the string you gave to pkill with -f. A process terminated by a signal exits with code 128 + the signal number. HUP is signal number 1, hence exit code 129.

share|improve this answer

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.