让程序立即在命令行返回,这样它就不会与启动它的 shell 绑定
有些程序从命令行启动后会立即返回,例如 Firefox。大多数实用程序(以及我编写的所有程序)都与创建它们的 shell 相关联。如果你在命令行中使用control-c,程序就会死掉。
您必须在程序或 shell 脚本中添加什么才能获得立即返回行为?我想我在那里问两个问题,一个是关于 shell 脚本的问题,一个是关于一般问题的,如果它们不同的话。我特别想知道是否有办法获得可执行 jar 来完成此操作。
我几乎不好意思问这个问题,但我自己却找不到答案。
谢谢!
Some programs return immediately when launched from the command line, Firefox for example. Most utilities (and all the programs I've written) are tied to the shell that created them. If you control-c the command line, the program's dead.
What do you have to add to a program or a shell script to get the return-immediately behavior? I guess I'm asking two questions there, one for shell scripts and one for general, if they're different. I would be particularly interested to know if there's a way to get an executable jar to do it.
I'm almost embarrassed to ask that one but I can't find the answer myself.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
在 Windows 上,
在 *nux 上
,这里替换
在 *nux 上,fg 和 bg 命令也是您的朋友......
on Windows,
on *nux
Here substitute
On *nux the fg and bg commands are your friends as well ...
您基本上需要
在 *nux 中分叉一个进程或创建一个新线程(或假装),您可以在命令后使用
&
来完成此操作,例如/long/script & ;
或者在 Windows 中,您可以创建一个执行进程然后退出的 BATCH 文件(它自然会执行此操作)。注意:在分叉后没有特别好的方法来引用该进程,基本上只有
ps
来获取进程列表。如果您希望能够查看进程正在执行的操作,请使用 screen(另一个 Linux 命令)进行查看,它将为您启动一个会话并让您“重新附加”到屏幕。为此,请安装 screen(
sudo apt-get install screen
或yum install screen
)。然后输入screen
创建一个新会话(注意,它看起来就像您没有执行任何操作)。然后,运行/long/command
(不带&
),然后按CTRL + A + D
(同时)与其分离(它仍在运行!)。然后,当您想要重新连接时,输入screen -r
。此外,请在任何帮助消息中查找允许您在不使用上述选项的情况下执行此操作的标志(例如,在
synergy
中,您可以说synergy --background
)You need to basically need to fork a process or create a new thread (or pretend to)
in *nux you can do this with an
&
after the command like this/long/script &
or in windows you can create a BATCH file that executes your processes then exits (it does this naturally).NOTE: there's no particularly good way to reference this process after you're forked it, basically only
ps
for the process list. if you want to be able to see what the process is doing, check out usingscreen
(another linux command) that will start a session for you and let you "re-attach" to the screen.to do this, install screen (
sudo apt-get install screen
oryum install screen
). then typescreen
to create a new session (note, it will look like you didn't do anything). then, run your/long/command
(without the&
), then pressCTRL + A + D
(at the same time) to detach from it (it's still running!). then, when you want to re-attach, typescreen -r
.Additionally, look for flags in any help message that allow you do this without using the above options (for instance in
synergy
you can saysynergy --background
)包装脚本仅包含以下内容:
将启动目标并立即退出。您可以将
nohup
添加到该行的开头,以便在退出 shell 时它将继续运行。A wrapper script consisting of nothing but:
Will launch the target and exit immediately. You can add
nohup
to the beginning of that line so it will continue running if the shell is exited.对于可执行程序(与 shell 脚本相反),在 Linux/Unix 上使用 fork() 和 exec(),然后退出父进程,这将返回到 shell。有关详细信息,请参阅手册页或某些页面,例如 http://www.yolinux.com/TUTORIALS /ForkExecProcesses.html。
For an executable program (as opposed to a shell script), on Linux/Unix use fork() and exec() and then exit the parent process, which will return to the shell. For details see the man pages, or some page like http://www.yolinux.com/TUTORIALS/ForkExecProcesses.html.