作为后台进程/服务运行命令
我有一个 Shell 命令,我想在后台运行,并且我读到这可以通过在命令后添加 &
来完成,这会导致它作为后台进程运行,但是我需要更多功能,并且想知道如何实现:
- 我希望命令在每次系统重新启动时在后台启动和运行。
- 我希望能够在需要时启动和停止它,就像
service apache2 start
一样。
我该怎么办?有没有一种工具可以让我将命令作为服务运行?
我对此有点迷失。
谢谢
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:
- 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
.
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
UNIX 系统可以同时处理您需要的任意多个进程(如果您使用的是 GUI,则只需打开新的 shell 窗口),因此仅当您需要继续使用当前 shell 窗口执行其他操作时,才需要在后台运行进程一旦您运行了一个持续运行的应用程序或进程。
要在后台模式下运行名为 command 的命令,您可以使用:
这是一个特殊字符,一旦进程启动,它将返回到命令提示符。还有其他特殊字符可以执行其他操作,更多信息请参见此处。
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.
To run a command called command in background mode, you'd use:
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.
看一下 daemon 命令,它可以将任意进程变成守护进程。这将允许您的脚本充当守护进程,而不需要您做很多额外的工作。下一步是在启动时自动调用它。要了解正确的方法,您需要提供您的操作系统(或者,对于 Linux,您的发行版)。
Take a look at the daemon command, 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).
基于这篇文章:
http:// felixmilea.com/2014/12/running-bash-commands-background-properly/...另一个好方法是使用
screen
例如:从 < a href="https://www.gnu.org/software/screen/manual/html_node/Invoking-Screen.html" rel="nofollow noreferrer">屏幕手册:
即您可以关闭终端,该进程将继续运行(与
&
不同),您也可以稍后重新附加到会话
Based on this article:
http:// felixmilea.com/2014/12/running-bash-commands-background-properly/...another good way is with
screen
eg:from the screen manual:
i.e. you can close your terminal, the process will continue running (unlike with
&
)with
screen
you can also reattach to the session later对于使用 bash 进行高级作业控制,您应该查看命令
jobs
bg
和fg
。但是,您似乎对在后台运行该命令并不真正感兴趣。您想要做的是在启动时启动该命令。执行此操作的方法因您使用的 Unix 系统而异,但请尝试查看
rc
系列文件(例如在 Ubuntu 上为/etc/rc.local
) 。它们包含将在 init 脚本之后执行的脚本。For advanced job control with bash, you should look into the commands
jobs
bg
andfg
.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.使用 nohup 将输出定向到 /dev/null
nohup command &>/dev/null &
Use nohup while directing the output to /dev/null
nohup command &>/dev/null &