Bash init - 在特定用户下启动服务
我正在尝试在 bash (Ubuntu) 中创建一个 init 脚本,该脚本在特定用户下启动服务。
除了这个之外还有更好的方法吗?
su - <user> -c "bash -c 'cd $DIR ;<service name>'"
I am trying to create an init script in bash (Ubuntu) that starts a service under a specific user.
Is there a better way to do that other than this?
su - <user> -c "bash -c 'cd $DIR ;<service name>'"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
Ubuntu 使用的
start-stop-daemon
已经支持此功能。使用 /etc/init.d 中的框架文件:
sudo cp /etc/init.d/sculpture /etc/init.d/mynewservice
适当地编辑 mynewservice。
将以下参数添加到调用 start-stop-daemon 的行中:
--chuid username:group
示例:
更改
start-stop-daemon --start --quiet --pidfile $PIDFILE --exec $DAEMON --测试> /dev/null \
到
start-stop-daemon --start --quiet --chuid someuser:somegroup --pidfile $PIDFILE --exec $DAEMON --test > /dev/null \
最后,注册您的服务并启动它:
update-rc.d mynewservice defaults 99 && service mynewservice start
有关 start-stop-daemon 其他选项的更多信息此处
Ubuntu uses
start-stop-daemon
which already supports this feature.Use the skeleton file from /etc/init.d:
sudo cp /etc/init.d/skeleton /etc/init.d/mynewservice
Edit mynewservice appropriately.
Add the following parameter to the lines that call start-stop-daemon:
--chuid username:group
Example:
Change
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 \
Finally, register your service and start it:
update-rc.d mynewservice defaults 99 && service mynewservice start
More info about other options for start-stop-daemon here
或者,您可以使用
/etc/init.d/functions
文件中定义的daemon
函数:如果您查看其语法,您可以执行其他操作,例如定义 PID 文件位置,设置良好的水平,等等。否则,它对于将服务作为守护进程启动非常有用。使用
daemon
启动的服务可以很容易地被另一个函数killproc
终止。Alternatively, you can use the
daemon
function defined in your/etc/init.d/functions
file: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
.您可以在
/etc/init.d/
下创建一个脚本,例如 your_service_name,其中内容最少#!/bin/sh
su - <用户>; -c "bash -c 'cd $DIR ;<服务名称>'"
为脚本提供适当的权限。现在,在所需的
/etc/rc.d
目录中使用update-rc.d
命令为您的脚本创建软链接,以便在以下情况下运行该脚本:系统以提到的运行级别启动。请参考
/etc/init.d/
下的脚本以供参考和使用。请参阅/etc/init.d/README
了解有关编写脚本的更多详细信息。update-rc.d
的手册页也将有助于了解update-rc.d
的用法。这绝对适用于我使用的 Ubuntu 机器,但我猜测该功能将在各个发行版上可用。希望这有帮助!
You can create a script under
/etc/init.d/
say your_service_name, with minimal contents#!/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 forupdate-rc.d
will also help to find out about the usage ofupdate-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!
我遇到了同样的问题,我通过
#!/bin/bash
su <用户>; -c "bash -c '<服务路径> $1'"
使用以下命令设置脚本的运行代码
sudo update-rc.d;默认
一旦在运行代码中设置此脚本,重新启动后,root 运行该脚本,并将启动/停止作为 $1 传递给脚本,具体取决于运行模式的状态。
如果您想在不重新启动的情况下测试脚本,您应该以 root 身份运行它并传递服务操作,例如启动/停止/重新启动......
root# ./etc/init.d/my_scrpit 启动
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
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