views:

66

answers:

2

I've written a shell script to soft-restart HAProxy (reverse proxy). Executing the script from the shell works. But I want a daemon to execute the script. That doens't work. system() returns 256. I have no clue what that might mean.

#!/bin/sh
# save previous state
mv /home/haproxy/haproxy.cfg /home/haproxy/haproxy.cfg.old
mv /var/run/haproxy.pid /var/run/haproxy.pid.old

cp /tmp/haproxy.cfg.new /home/haproxy/haproxy.cfg
kill -TTOU $(cat /var/run/haproxy.pid.old)
if haproxy -p /var/run/haproxy.pid -f /home/haproxy/haproxy.cfg; then
  kill -USR1 $(cat /var/run/haproxy.pid.old)
  rm -f /var/run/haproxy.pid.old
  exit 1
else
  kill -TTIN $(cat /var/run/haproxy.pid.old)
  rm -f /var/run/haproxy.pid
  mv /var/run/haproxy.pid.old /var/run/haproxy.pid
  mv /home/haproxy/haproxy.cfg /home/haproxy/haproxy.cfg.err
  mv /home/haproxy/haproxy.cfg.old /home/haproxy/haproxy.cfg
  exit 0
fi

HAProxy is executed with user haproxy. My daemon has it's own user too. Both run with sudo.

Any hints?

+1  A: 

According to this and that, Perl's system() returns exit values multiplied by 256. So it's actually exiting with 1. It seems this happens in C too.

Skilldrick
Thanks! So it's EPERM /* Operation not permitted */ ... but why ... I start the daemon with sudo.
Fair Dinkum Thinkum
I don't know if I'm understanding the question correctly, but doesn't the shell script exit with `1` `if haproxy -p /var/run/haproxy.pid -f /home/haproxy/haproxy.cfg;` ? Is that not where the `1` is coming from?
Skilldrick
Ouch! Now I got it :)
Fair Dinkum Thinkum
Ah good :) Happy to help.
Skilldrick
+1  A: 

Unless system returns -1 its return value is of the same format as the status value from the wait family of system calls (man 2 wait). There are macros to help you interpret this status:

man 3 wait

Lists these macros and what they tell you.

nategoose