Linux Bash init - 在特定用户下启动服务
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7465842/
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
Bash init - start service under specific user
提问by David Ryder
I am trying to create an init script in bash (Ubuntu) that starts a service under a specific user.
我正在尝试在 bash (Ubuntu) 中创建一个 init 脚本,该脚本在特定用户下启动服务。
Is there a better way to do that other than this?
除了这个,还有没有更好的方法来做到这一点?
su - <user> -c "bash -c 'cd $DIR ;<service name>'"
回答by Manny D
Alternatively, you can use the daemon
function defined in your /etc/init.d/functions
file:
或者,您可以使用文件中daemon
定义的函数/etc/init.d/functions
:
daemon --user=<user> $DIR/program
If you look into its syntax you can do other things like defining PID file location, setting nice level, and whatnot. It's otherwise really useful for starting up services as daemons. Services started up with daemon
can easily be terminated by another functions function, killproc
.
如果你查看它的语法,你可以做其他事情,比如定义 PID 文件位置、设置好级别等等。否则,它对于将服务作为守护程序启动非常有用。启动的服务daemon
可以很容易地被另一个功能函数终止killproc
。
回答by another.anon.coward
You can create a script under /etc/init.d/
say your_service_name, with minimal contents
您可以在/etc/init.d/
说 your_service_name下创建一个脚本,内容最少
#!/bin/sh
su - <user> -c "bash -c 'cd $DIR ;<service name>'"
#!/bin/sh
su - <user> -c "bash -c 'cd $DIR ;<service name>'"
Provide appropriate permission to the script. Now use update-rc.d
command in the required /etc/rc<run_level>.d
directory to create a soft link for your script so that the script is run when system starts with the mentioned run level.
Please refer scripts under /etc/init.d/
for reference & please go through /etc/init.d/README
for more details regarding writing the script. Man page for update-rc.d
will also help to find out about the usage of update-rc.d
. This definitely works on Ubuntu machine I use, but I'm guessing that this facility will be available across distros.
Hope this helps!
为脚本提供适当的权限。现在update-rc.d
在所需/etc/rc<run_level>.d
目录中使用命令为您的脚本创建一个软链接,以便在系统以上述运行级别启动时运行该脚本。
请参阅下面的脚本以/etc/init.d/
供参考,请仔细阅读/etc/init.d/README
有关编写脚本的更多详细信息。手册页update-rc.d
也将有助于了解update-rc.d
. 这绝对适用于我使用的 Ubuntu 机器,但我猜这个工具将在发行版中可用。
希望这可以帮助!
回答by Ali
I had the same issue and I solved it by
我有同样的问题,我解决了
Create bash script and put it in /etc/init.d/ using following pattern
#!/bin/bash
su <user> -c "bash -c '<path to service> $1'"
Setup run codes for the script using the following command
sudo update-rc.d <my_scrpit> defaults
创建 bash 脚本并将其放入 /etc/init.d/ 使用以下模式
#!/bin/bash
su <user> -c "bash -c '<path to service> $1'"
使用以下命令为脚本设置运行代码
sudo update-rc.d <my_scrpit> defaults
Once this script is set in run codes, upon restarting, root run the script and will pass start/stop to the script as $1, depending on the status of the run mode.
If you want to test your script without restarting you should run it as root and pass the service action e.g. start/stop/restart.... root# ./etc/init.d/my_scrpit start
在运行代码中设置此脚本后,在重新启动时,root 运行脚本并将启动/停止作为 $1 传递给脚本,具体取决于运行模式的状态。
如果你想在不重启的情况下测试你的脚本,你应该以 root 身份运行它并传递服务操作,例如启动/停止/重启....root# ./etc/init.d/my_scrpit start
回答by Carl
Ubuntu uses start-stop-daemon
which already supports this feature.
Ubuntu 使用start-stop-daemon
它已经支持此功能。
Use the skeleton file from /etc/init.d:
使用 /etc/init.d 中的骨架文件:
sudo cp /etc/init.d/skeleton /etc/init.d/mynewservice
sudo cp /etc/init.d/skeleton /etc/init.d/mynewservice
Edit mynewservice appropriately.
适当地编辑 mynewservice。
Add the following parameter to the lines that call start-stop-daemon:
将以下参数添加到调用 start-stop-daemon 的行中:
--chuid username:group
--chuid username:group
Example:
例子:
Change
改变
start-stop-daemon --start --quiet --pidfile $PIDFILE --exec $DAEMON --test > /dev/null \
start-stop-daemon --start --quiet --pidfile $PIDFILE --exec $DAEMON --test > /dev/null \
to
到
start-stop-daemon --start --quiet --chuid someuser:somegroup --pidfile $PIDFILE --exec $DAEMON --test > /dev/null \
start-stop-daemon --start --quiet --chuid someuser:somegroup --pidfile $PIDFILE --exec $DAEMON --test > /dev/null \
Finally, register your service and start it:
最后,注册您的服务并启动它:
update-rc.d mynewservice defaults 99 && service mynewservice start
update-rc.d mynewservice defaults 99 && service mynewservice start
More info about other options for start-stop-daemon here
有关 start-stop-daemon 其他选项的更多信息,请点击此处