在 upstart 脚本中切换用户

Switch between users in an upstart script

是否可以有一个 upstart 脚本,它以 root 身份运行前脚本,而以 normal_user 身份运行其余脚本。我正在尝试类似的东西:

setuid normal_user

pre-start exec su -c "echo I'm root" root

script
exec /bin/bash <<"EOT"
    echo "haha, I'm normal user"
EOT

是否需要删除setuid?

我终于通过删除 setuid normal_user 并更改

使其正常工作
exec /bin/bash <<"EOT"

exec sudo -u normal_user /bin/bash <<"EOT"

通常,您必须删除 setuid 节,以便您的作业以 root 身份运行。然后,您可以在 exec/script 节中删除权限。

来自 Upstart Cookbook 的 Changing User 部分:

The recommended method for Debian and Ubuntu systems is to use the helper utility start-stop-daemon(8) like this:
exec start-stop-daemon --start -c myuser --exec command

If you want to use su(1)... To avoid the fork(2) caused by the shell being spawned, you could instead specify:
exec su -s /bin/sh -c 'exec "[=14=]" "$@"' $user -- /path/to/command --arg1=foo -b wibble

A basic example using sudo(8):
exec sudo -u $user command