简体   繁体   English

Ubuntu在启动时启动Rails

[英]Ubuntu start up Rails on boot up

I want to start my Rails server with the following command on boot up: 我想在启动时使用以下命令来启动我的Rails服务器:

cd /home/ubuntu/app && bundle exec rails server -p 8080 -e production

I placed it inside the /etc/rc.local, but it never starts up the server. 我将其放在/etc/rc.local中,但它从未启动过服务器。 If I remove bundle exec then shell complains about not finding the rails command. 如果我删除了bundle exec那么shell将抱怨找不到rails命令。

Both variants, with and without bundle exec , work if I log in, and execute it manually. 不论是否带有bundle exec ,这两种变体都可以在我登录并手动执行时使用。

I am totally lost. 我完全迷路了。 Any suggestions? 有什么建议么?

尝试更改用户

su ubuntu && cd /home/ubuntu/app && bundle exec rails server -p 8080 -e production

Running your script as root is not a good idea, so you want to change user before starting your code. 以root身份运行脚本不是一个好主意,因此您想在启动代码之前更改用户。

su is indeed the command you want to use, but you need to realise that all arguments are passed to the login shell. su确实是您要使用的命令,但是您需要意识到所有参数都已传递到登录外壳。 So the following won't work (assuming fofox is your username): 因此以下操作将无效(假设fofox是您的用户名):

su fofox pwd

as you're effectively saying 就像你说的那样

/bin/bash /bin/pwd

but /bin/pwd does not contain a list of shell commands, so both will give an error message: 但是/bin/pwd不包含Shell命令列表,因此两者都将给出错误消息:

/bin/pwd: /bin/pwd: cannot execute binary file

Thus you need to add something to the shell saying you're passing a list of commands, like this: 因此,您需要在shell中添加一些内容,例如要传递命令列表,如下所示:

/bin/bash -c /bin/pwd

The last wrinkle is that you want to pass a number of arguments to the su command but protect them from the shell, as 最后一点是,您希望将许多参数传递给su命令,但要保护它们不受shell的影响,例如

su fofox -c id && id

will show you that the first bit is executed as fofox and the second part as root again. 会告诉您第一位以fofox身份执行,第二位再次以root身份执行。 Single quotes are used to prevent the root shell to see the && characters. 单引号用于防止根外壳看到&&字符。

So the final command becomes: 因此,最终命令变为:

su fofox -c 'cd /home/ubuntu/app && bundle exec rails server -p 8080 -e production'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM