0

I'm looking at an init.d script written by a previous coworker which is not functioning correctly now. It's running on centos-6

After some experimentation I found the cause of the issue. He wrote the script to wait up to 30 seconds for a kill -TERM command to runciton before reporting an error. To do this he is running the command:

kill -0 $pid

to test if the killed program has died yet.

The kill -0 command is throwing an error:

/etc/init.d/celerybeat line 211: kill (<pid>) - No such process

it then exists immediately after this line fails.

This is what I would expect with set -e, but the script does not run set -e. In fact it has the line commented out with a comment saying that the kill -0 command will fail if it's set, so he was very aware he didn't want it set.

This happens rather I run it via service or run the script direclty from the /etc/init.d directory

So..why is the script running as if set -e is set? and more importantly, how do I prevent it from running that way, or rewrite the line to check if the pid is running without throwing an exception?

1

You can disable the "exit on error" with set +e or set +o errexit, or run the command in a conditional so the error exit doesn't apply. Simplest would be:

somecmd ... || true

But that would destroy the return value, and you probably need it as kill -0 doesn't do much except set it. So maybe something like this:

if kill -0 "$pid" 2>/dev/null ; then
    echo it lives
fi

As for the "why", I think CentOS 6 runs Upstart, and at least some Ubuntu documentation seems to state that Upstart runs scripts with set -e enabled.

  • By pure chance I was looking into CentOS upstart the other day and from what I could tell centos 7 runs it, but not centos 6. – dsollen Jul 25 '17 at 15:36
  • @dsollen, what I can find (i.e. Wikipedia) says that RHEL/CentOS 6 uses upstart, and RHEL/CentOS 7 uses systemd. But of course you know your system best. – ilkkachu Jul 25 '17 at 18:07

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.