bash 以不同的用户身份在 su 中永远运行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18940512/
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
Running forever within su as a different user
提问by lwood
I am experiencing weird behavior with forever, which I want to use to keep alive my node app.
我永远都在经历奇怪的行为,我想用它来保持我的节点应用程序的活动状态。
I want to run my forever processes as my regular user lwood, not as root.
我想以普通用户的身份运行我的永久进程lwood,而不是root.
I need to know how to run forever properly within rootmode, but as the user lwood. (This is needed because, for example as a special case, upstart scripts run as root.)
我需要知道如何在root模式下永远正确运行,但作为用户lwood. (这是必需的,因为例如作为一种特殊情况,新贵脚本以 root 身份运行。)
These commands illustrate my problem (I'm on Ubuntu 12.04, and $is regular user and #is root):
这些命令说明了我的问题(我使用的是 Ubuntu 12.04,并且$是普通用户并且#是 root):
$ su
[type in su password]
# cd /home/lwood/myapp
# sudo -u lwood forever -a -l "/home/lwood/myapp/logfile.log" start app.js
info: Forever processing file: app.js
# forever list
info: No forever processes running
# exit
$ forever list
info: No forever processes running
So forever successfully started, yet no processes are running under neither lwood nor root!
所以永远成功启动,但没有进程在 lwood 和 root 下运行!
How can I fix this problem?
我该如何解决这个问题?
采纳答案by Tony Chen
If you're using upstart, try this (putting it to your upstart script)
如果你正在使用 upstart,试试这个(把它放到你的 upstart 脚本中)
exec su -s /bin/sh -c 'exec "$0" "$@"' username -- /usr/local/bin/forever ...
exec su -s /bin/sh -c 'exec "$0" "$@"' 用户名 -- /usr/local/bin/forever ...
reference: https://superuser.com/questions/213416/running-upstart-jobs-as-unprivileged-users
参考:https: //superuser.com/questions/213416/running-upstart-jobs-as-unprivileged-users
回答by fraber
On systems with systemd (RHEL 7, CentOS 7, Debian, Ubuntu, Fedora, ...) you can use this script as /usr/lib/systemd/system/nodejs.service or equivalent (check for other *.service files):
在带有 systemd 的系统(RHEL 7、CentOS 7、Debian、Ubuntu、Fedora 等)上,您可以将此脚本用作 /usr/lib/systemd/system/nodejs.service 或等效的(检查其他 *.service 文件) :
[Unit]
Description=Node.js Application
After=postgresql.service network.target
Wants=postgresql.service
[Service]
ExecStart=/usr/bin/su - <user> -c '/usr/bin/npm start --prefix /path/to/app'
WorkingDirectory=/path/to/app
Restart=always
RestartSec=30
[Install]
WantedBy=multi-user.target
回答by konsolebox
Probably because it's running as a different context on a different user. Maybe you need to add sudo a swell for the list:
可能是因为它在不同用户的不同上下文中运行。也许您需要为列表添加 sudo 膨胀:
sudo -u lwood forever list

