3

I've inherited quite a bit of code and am looking at a cron job that restarts a service once an hour, no other scripts touch this process and this code is ran.

#The name of the process has been scrubbed to protect the guilty

procpid=$( pidof proc )
if [ -n "$procpid" ]; then
  kill -HUP $procpid
  procpid=$( pidof proc )
  if [ -z "$procpid" ]; then
    error "PROC ain't running, go figure out why"
  fi
fi

99.9999% of the time this works. Problem is, I'm a five 9's kind of guy who hates inexplicable syslog messages being emailed to me and I want to know why:

kill: (1076) - No such process

keeps popping up in there. Does this necessarily mean that something else is killing my process between the "if" and the "kill" or could something more insidious be going on?


Because in all likelihood this code is fine and in fact something else kills the process between the two lines, is there "what killed me" diagnostic sort of checker thing somewhere that I could use to at least debug why I'm getting these warnings?

  • A process can catch the SIGHUP signal and then log the pid that sent the signal. Are you able to modify proc's code? If so, what language is it written in? – Mark Plotnick Apr 24 '15 at 0:19
2

Without knowing any more information, especially the nature of the process involved, I would have to say, yes, I guess it is dying between the pidof and kill. You could use killall instead of basically your whole block of code. The race condition would be still present, but the window would be shorter.

Because in all likelihood this code is fine and in fact something else kills the process between the two lines, is there "what killed me" diagnostic sort of checker thing somewhere that I could use to at least debug why I'm getting these warnings?

Signal-sending isn't logged in UNIX, so you cannot in general know "what killed me". What you could perhaps do easily, at least, is run the process in question in a wrapper shell script. That can at least log the reason why the process died.

#!/bin/sh

proc
logger "proc died with status $?"

$? will be between 0 and 127 if the process exited normally and greater than 128 if it died due to receiving a signal, with the signal number being $?-128.

  • OK that's good and it still works with -HUP. Gonna edit my questions because I figured that would be the answer. – Peter Turner Apr 23 '15 at 16:48

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.