Linux 使用 system() 执行 shell 脚本返回 256。这是什么意思?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3736320/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 23:34:47  来源:igfitidea点击:

Executing shell script with system() returns 256. What does that mean?

clinuxshelldaemon

提问by Jan Deinhard

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.

我已经编写了一个 shell 脚本来软重启 HAProxy(反向代理)。从 shell 执行脚本有效。但我想要一个守护进程来执行脚本。那行不通。system()返回 256。我不知道这可能意味着什么。

#!/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.

HAProxy 由用户 haproxy 执行。我的守护进程也有自己的用户。两者都使用 sudo 运行。

Any hints?

任何提示?

采纳答案by Skilldrick

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

根据这个那个,Perlsystem()返回的退出值乘以 256。所以它实际上是用 退出1。这似乎也发生在 C 中

回答by nategoose

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:

除非系统返回 -1,否则其返回值的格式与系统调用的等待系列 (man 2 wait) 中的状态值的格式相同。有一些宏可以帮助您解释此状态:

man 3 wait

Lists these macros and what they tell you.

列出这些宏以及它们告诉您的内容。

回答by user2000703

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!

代码 256 可能意味着系统命令无法找到二进制文件来运行它。请记住,它可能不会调用 bash 并且可能没有设置路径。使用二进制文件的完整路径再试一次!

回答by Xiaofeng

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.

在守护进程中调用包含“kill”命令的脚本时,我遇到了同样的问题。守护进程必须关闭标准输出,标准错误...使用类似 system("scrips.sh > /dev/null") 的东西应该可以工作。