作为后台进程/服务运行命令

发布于 2024-12-16 14:46:27 字数 280 浏览 1 评论 0原文

我有一个 Shell 命令,我想在后台运行,并且我读到这可以通过在命令后添加 & 来完成,这会导致它作为后台进程运行,但是我需要更多功能,并且想知道如何实现:

  1. 我希望命令在每次系统重新启动时在后台启动和运行。
  2. 我希望能够在需要时启动和停止它,就像 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:

  1. I'd like the command to start and run in the background every time the system restarts.
  2. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

攒一口袋星星 2024-12-23 14:46:27

UNIX 系统可以同时处理您需要的任意多个进程(如果您使用的是 GUI,则只需打开新的 shell 窗口),因此仅当您需要继续使用当前 shell 窗口执行其他操作时,才需要在后台运行进程一旦您运行了一个持续运行的应用程序或进程。

要在后台模式下运行名为 command 的命令,您可以使用:

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:

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.

疯到世界奔溃 2024-12-23 14:46:27

看一下 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).

Oo萌小芽oO 2024-12-23 14:46:27

基于这篇文章:
http:// felixmilea.com/2014/12/running-bash-commands-background-properly/

...另一个好方法是使用 screen 例如:

screen -d -m -s "my session name" <command to run>

从 < a href="https://www.gnu.org/software/screen/manual/html_node/Invoking-Screen.html" rel="nofollow noreferrer">屏幕手册:

<代码>-d -m
以分离模式启动屏幕。这会创建一个新会话,但不会附加到它。这对于系统启动脚本很有用。

即您可以关闭终端,该进程将继续运行(与 & 不同),

您也可以稍后重新附加到会话

Based on this article:
http:// felixmilea.com/2014/12/running-bash-commands-background-properly/

...another good way is with screen eg:

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.

i.e. you can close your terminal, the process will continue running (unlike with &)

with screen you can also reattach to the session later

茶底世界 2024-12-23 14:46:27

对于使用 bash 进行高级作业控制,您应该查看命令 jobs bgfg

但是,您似乎对在后台运行该命令并不真正感兴趣。您想要做的是在启动时启动该命令。执行此操作的方法因您使用的 Unix 系统而异,但请尝试查看 rc 系列文件(例如在 Ubuntu 上为 /etc/rc.local) 。它们包含将在 init 脚本之后执行的脚本。

For advanced job control with bash, you should look into the commands jobs bg and 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.

黑凤梨 2024-12-23 14:46:27

使用 nohup 将输出定向到 /dev/null

nohup command &>/dev/null &

Use nohup while directing the output to /dev/null

nohup command &>/dev/null &

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文