在初始化脚本中切换用户?
这是我的 Ubuntu 工作站上的初始化脚本。我需要以除 root 之外的其他用户身份运行命令,但我就是不知道应该如何完成它。 sudo -u
或 su newuser
似乎都不起作用。
脚本:
respawn
console none
start on runlevel [2345]
stop on runlevel [06]
script
su "anotherUser" -c ./myCommand
end script
Here's my Init script which I have at my Ubuntu workstation. I need to run a command as another user than root, but I just can't get my head around how it should be done. Neither sudo -u
or su newuser
seems to work.
The script:
respawn
console none
start on runlevel [2345]
stop on runlevel [06]
script
su "anotherUser" -c ./myCommand
end script
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我使用这个:
更新:由于有人对此答案感兴趣,我在这里解释一下我使用它的方式。
我们以普通 Linux 用户身份运行服务器,而不是 root。用户名包含三部分:
service、customer、stage
这样我们就可以在一个 Linux 操作系统中为多个客户运行多个服务。
示例: foo_bar_p 客户“bar”的服务“foo”,“p”表示生产
以下是初始化脚本的部分。初始化脚本可以作为 root 或 foo_bar_p 用户执行:
I use this:
Update: Since there is interest in this answer, I explain the way I use it here.
We run servers as normal linux users, not root. The username contains three parts:
service, customer, stage
This way we can run several services for several customers in one linux OS.
Example: foo_bar_p Service "foo" of customer "bar" and "p" means production
Here is the part of the init script. The init script can be executed as root or as foo_bar_p user:
对于新贵,请使用:
For upstart, use:
su 可能是一种更通用的方法,但这在一些带有 sudo 的常见发行版上也是可能的:(
只需重读您的问题,并没有意识到这对您不起作用,但它在初始化脚本中对我有用)
su is probably a more universal approach, but this is also possible on some common distributions with sudo:
(just reread your question and didn't realize that doesn't work for you, but it has worked for me in init scripts)