在以用户身份登录之前,在启动时运行 bash 脚本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18188192/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 06:13:19  来源:igfitidea点击:

Run a bash script on startup, before login as a user

bashubuntu-12.04startupubuntu-server

提问by ms.Ashlee

I am trying to have my Ubuntu 12.04 LTS server run a bash script I have to start a Minecraft server on start up, prior to log in but as user minecraft. I can have it run as rootby placing the following in /etc/rc.local

我试图让我的 Ubuntu 12.04 LTS 服务器运行一个 bash 脚本我必须在启动时启动一个 Minecraft 服务器,在登录之前但作为用户minecraft。我可以root通过放置以下内容来运行它/etc/rc.local

bash /path/to/script/script.sh

which runs the script as root, I have tried the following in /etc/rc.local

运行脚本作为root,我已经尝试了以下/etc/rc.local

su -c `bash /path/to/script/script.sh` minecraft

but to no avail. Can anyone tell me what I am doing wrong or should be doing instead? The first line of my script is

但无济于事。谁能告诉我我做错了什么或应该做什么?我的脚本的第一行是

#!/bin/bash

in case it is important. Thanks much!

以防万一。非常感谢!

回答by jtravaglini

Try

尝试

su minecraft -c '/bin/bash /path/to/script/script.sh &'
  • The user should be the first argument to su.
  • You should use quotes and not ticks for the command argument (-c)
  • You may want to consider using su -l minecraftto have the script run in an environment which would be similar to that if the user minecraft logged in directly.
  • 用户应该是 su 的第一个参数。
  • 对于命令参数 (-c),您应该使用引号而不是刻度
  • 您可能需要考虑使用su -l minecraft让脚本在类似于用户 minecraft 直接登录的环境中运行。

Give this a shot and let me know if it works.

试一试,让我知道它是否有效。