Executing shell script with system() retur

2019-01-09 06:37发布

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 doesn'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?

4条回答
Summer. ? 凉城
2楼-- · 2019-01-09 07:09

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.

查看更多
祖国的老花朵
3楼-- · 2019-01-09 07:11

A code of 256 probably means that the system command cannot locate the binary to run it. Remember that it may not be calling bash and that it may not have paths setup. Try again with full paths to the binaries!

查看更多
你好瞎i
4楼-- · 2019-01-09 07:23

I have the same problem when call script that contains `kill' command in a daemon. The daemon must have closed the stdout, stderr... Use something like system("scrips.sh > /dev/null") should work.

查看更多
放我归山
5楼-- · 2019-01-09 07:32

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.

查看更多
登录 后发表回答