如何告诉 start-stop-daemon 根据 --chuid 参数更新 $HOME 和 $USER

iEl*_*ric 6 linux users home-directory environment-variables

我正在尝试运行使用$HOME$USER环境变量的服务。我可以将它们设置为服务本身,但这只是一个临时解决方案。

假设我有一个test.sh包含以下内容的脚本:

echo $USER
Run Code Online (Sandbox Code Playgroud)

我运行它start-stop-daemon以查看我的结果:

$ start-stop-daemon --start --exec `pwd`/test.sh --user guest --group guest --chuid -guest
root
Run Code Online (Sandbox Code Playgroud)

好像它没有更新环境,也许应该报告为错误?

我发现了一个讨厌的hacky解决方案,它只在我这个简单的用例中有效(原因不明):

$ start-stop-daemon --exec /usr/bin/sudo --start -- -u guest -i 'echo $USER'
guest
Run Code Online (Sandbox Code Playgroud)

我敢肯定其他人偶然发现了这一点,我对干净的解决方案感兴趣。

$ start-stop-daemon --version
start-stop-daemon 1.13.11+gentoo
Run Code Online (Sandbox Code Playgroud)

Jam*_*ger 6

这可能是预期的行为。手册页显示了一个--env选项start-stop-daemon

   -e|--env env-name
          Set an environment variable whose name  and  value  is  env-name
          before   starting  executable.   Example:  -e  HOME="/home/user"
          exports an environment variable whose name is  HOME  with  value
          "/home/user".   Note,  only  one  --env  option is suppoted, use
          /usr/bin/env if you need more.
Run Code Online (Sandbox Code Playgroud)

$HOME示例中使用的作者,我认为这意味着它通常不会设置它。我没有看到任何其他选项可用于更新您正在启动的进程的环境。

尝试start-stop-daemon像这样运行:

USER=guest HOME=~guest start-stop-daemon --start --exec /path/to/prog ...
Run Code Online (Sandbox Code Playgroud)

另一种选择是在 下运行脚本sudo

start-stop-daemon --start --exec /usr/bin/sudo -- -H -u guest /path/to/prog
Run Code Online (Sandbox Code Playgroud)

sudo将自动设置$USER,并且-H选项告诉它也设置$HOME。我用我自己的test.sh打印这些变量的值运行了这两个,并且都根据需要更新了它们。我偏爱第一个,因为它不会在混合中添加另一个程序,但这只是我。