让程序立即在命令行返回,这样它就不会与启动它的 shell 绑定

发布于 2024-08-19 08:09:24 字数 263 浏览 3 评论 0原文

有些程序从命令行启动后会立即返回,例如 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 技术交流群。

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

发布评论

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

评论(4

国际总奸 2024-08-26 08:09:24
start cmd 

在 Windows 上,

cmd &

在 *nux 上

,这里替换

cmd = java -jar JarFile.jar

在 *nux 上,fgbg 命令也是您的朋友......

start cmd 

on Windows,

cmd &

on *nux

Here substitute

cmd = java -jar JarFile.jar

On *nux the fg and bg commands are your friends as well ...

一袭白衣梦中忆 2024-08-26 08:09:24

您基本上需要

在 *nux 中分叉一个进程或创建一个新线程(或假装),您可以在命令后使用 & 来完成此操作,例如 /long/script & ; 或者在 Windows 中,您可以创建一个执行进程然后退出的 BATCH 文件(它自然会执行此操作)。

注意:在分叉后没有特别好的方法来引用该进程,基本上只有 ps 来获取进程列表。如果您希望能够查看进程正在执行的操作,请使用 screen(另一个 Linux 命令)进行查看,它将为您启动一个会话并让您“重新附加”到屏幕。

为此,请安装 screen(sudo apt-get install screenyum 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 using screen (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 or yum install screen). then type screen to create a new session (note, it will look like you didn't do anything). then, run your /long/command (without the &), then press CTRL + A + D (at the same time) to detach from it (it's still running!). then, when you want to re-attach, type screen -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 say synergy --background)

南七夏 2024-08-26 08:09:24

包装脚本仅包含以下内容:

your_prog_or_script &

将启动目标并立即退出。您可以将 nohup 添加到该行的开头,以便在退出 shell 时它将继续运行。

A wrapper script consisting of nothing but:

your_prog_or_script &

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.

孤独岁月 2024-08-26 08:09:24

对于可执行程序(与 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.

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