php 通过 bash 在 linux 中产生一个完全独立的进程
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2731568/
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
spawn an entirely separate process in linux via bash
提问by Jay Elston
I need to have a script execute (bash or perl or php, any will do) another command and then exit, while the other command still runs and exits on its own. I could schedule via at command, but was curious if there was a easier way.
我需要让脚本执行(bash 或 perl 或 php,任何都可以)另一个命令然后退出,而另一个命令仍然运行并自行退出。我可以通过 at 命令进行调度,但很好奇是否有更简单的方法。
回答by Ernelli
#!/bin/sh
your_cmd &
echo "started your_cmd, now exiting!"
Similar constructs exists for perl and php, but in sh/bash its very easy to run another command in the background and proceed.
perl 和 php 存在类似的构造,但在 sh/bash 中,在后台运行另一个命令并继续操作非常容易。
edit
编辑
A very good source for generic process manipulation are all the start scripts under /etc/init.d. They do all sorts of neat tricks such as keep track of pids, executing basic start/stop/restart commands etc.
通用进程操作的一个很好的来源是/etc/init.d. 他们做了各种巧妙的技巧,例如跟踪 pid、执行基本的启动/停止/重启命令等。
回答by Jay Elston
To run a command in the background, you can append an '&' to the command.
要在后台运行命令,您可以在命令后附加一个“&”。
If you need the program to last past your login session, you can use nohup.
如果您需要该程序持续超过您的登录会话,您可以使用nohup。
See this similar stackoverflow discussion: how to run a command in the background ...
请参阅此类似的 stackoverflow 讨论:如何在后台运行命令...
回答by camh
The usual way to run a command and have it keep running when you log out is to use nohup(1). nohupprevents the given command from receiving the HUP signal when the shell exits. You also need to run in the background with the ampersand (&) command suffix.
运行命令并让它在您注销时继续运行的常用方法是使用nohup(1). nohup防止给定的命令在 shell 退出时接收 HUP 信号。您还需要使用与号 (&) 命令后缀在后台运行。
$ nohup some_command arg1 arg2 &
回答by mob
&?
&?
#!/usr/bin/bash
# command1.sh: execute command2.sh and exit
command2.sh &
回答by Tibrim
I'm not entirely sure if this is what you are looking for, but you can background a process executed in a shell by appending the ampersand (&) symbol as the last character of the command.
我不完全确定这是否是您要查找的内容,但是您可以通过附加与号 (&) 符号作为命令的最后一个字符来后台执行在 shell 中执行的进程。
So if you have script, a.sh
所以如果你有脚本,a.sh
and a.sh needs to spawn a seperate process, like say execute the script b.sh, you'd:
并且 a.sh 需要产生一个单独的进程,比如执行脚本 b.sh,你会:
b.sh &
回答by cikkle
So long as you mentioned Perl:
只要你提到 Perl:
fork || exec "ls";
...where "ls" is anything at all. Repeat for as many commands as you need to fire off.
...其中“ls”是什么。重复执行您需要触发的任意数量的命令。

