bash 将命令作为后台进程/服务运行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8164664/
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 a command as a background process/service
提问by Mridang Agarwalla
I have a Shell command that I'd like to run in the background and I've read that this can be done by suffixing an &
to the command which causes it to run as a background process but I need some more functionality and was wondering how to go about it:
我有一个想在后台运行的 Shell 命令,我读到这可以通过&
在命令后缀 an来完成,这会导致它作为后台进程运行,但我需要更多功能,并且想知道如何去做吧:
- I'd like the command to start and run in the background every time the system restarts.
- I'd like to be able to able to start and stop it as and when needed just like one can do
service apache2 start
.
- 我希望每次系统重新启动时命令都在后台启动和运行。
- 我希望能够像人们一样在需要时启动和停止它
service apache2 start
。
How can I go about this? Is there a tool that allows me to run a command as a service?
我该怎么办?是否有允许我将命令作为服务运行的工具?
I'm a little lost with this.
我有点迷失了。
Thanks
谢谢
回答by MattMatt
UNIX systems can handle as many processes as you need simultaneously (just open new shell windows if you're in a GUI), so running a process in the background is only necessary if you need to carry on using the current shell window for other things once you've run an application or process that keeps running.
UNIX 系统可以同时处理任意数量的进程(如果您在 GUI 中,只需打开新的 shell 窗口),因此只有在需要继续使用当前 shell 窗口处理其他事情时才需要在后台运行进程一旦你运行了一个持续运行的应用程序或进程。
To run a command called commandin background mode, you'd use:
要在后台模式下运行名为command 的命令,您可以使用:
command &
This is a special character that returns you to the command prompt once the process is started. There are other special characters that do other things, more info is available here.
这是一个特殊字符,一旦进程启动,您就会返回到命令提示符。还有其他特殊字符可以做其他事情,更多信息可在此处获得。
回答by sorpigal
Take a look at the daemoncommand, which can turn arbitrary processes into daemons. This will allow your script to act as a daemon without requiring you to do a lot of extra work. The next step is to invoke it automatically at boot. To know the correct way to do that, you'll need to provide your OS (or, for Linux, your distribution).
看看daemon命令,它可以将任意进程变成守护进程。这将允许您的脚本充当守护程序,而无需您做很多额外的工作。下一步是在启动时自动调用它。要知道正确的方法,您需要提供您的操作系统(或者,对于 Linux,您的发行版)。
回答by Anentropic
Based on this article:http:// felixmilea.com/2014/12/running-bash-commands-background-properly/
基于这篇文章:http://felixmilea.com/2014/12/running-bash-commands-background-properly/
...another good way is with screen
eg:
...另一个好方法是screen
例如:
screen -d -m -s "my session name" <command to run>
from the screen manual:
从屏幕手册:
-d -m
Start screen in detached mode. This creates a new session but doesn't attach to it. This is useful for system startup scripts.
-d -m
分离模式下的开始屏幕。这会创建一个新会话,但不会附加到它。这对于系统启动脚本很有用。
i.e. you can close your terminal, the process will continue running (unlike with &
)
即您可以关闭终端,该进程将继续运行(与 不同&
)
with screen
you can also reattach to the session later
与screen
你也可以重新连接到会议后
回答by rahmu
For advanced job control with bash, you should look into the commands jobs
bg
and fg
.
对于使用 bash 的高级作业控制,您应该查看命令jobs
bg
和fg
.
However, it seems like you're not really interested in running the command in the background. What you want to do is launch the command at startup. The way to do this varies depending on the Unix system you use, but try to look into the rc
family of files (/etc/rc.local
for example on Ubuntu). They contain scripts that will be executed after the init script.
但是,您似乎对在后台运行命令并不真正感兴趣。您要做的是在启动时启动命令。执行此操作的方法因您使用的 Unix 系统而异,但请尝试查看rc
文件系列(/etc/rc.local
例如在 Ubuntu 上)。它们包含将在 init 脚本之后执行的脚本。
回答by philip mudenyo
Use nohup while directing the output to /dev/null
在将输出定向到 /dev/null 时使用 nohup
nohup command &>/dev/null &
nohup command &>/dev/null &