Mat*_*hew 398 command-line process terminal background-process
有时我想开始一个过程并忘记它。如果我从命令行启动它,就像这样:
redshift
Run Code Online (Sandbox Code Playgroud)
我无法关闭终端,否则它会终止进程。我可以以可以关闭终端而不终止进程的方式运行命令吗?
Ste*_*n D 415
以下 2 项之一应该有效:
$ nohup redshift &
Run Code Online (Sandbox Code Playgroud)
或者
$ redshift &
$ disown
Run Code Online (Sandbox Code Playgroud)
有关其工作原理的更多信息,请参阅以下内容:
man nohup
help disown
nohup、disown 和 & 之间的区别(一定要阅读评论)
Ste*_*fan 161
如果你的程序已经在运行,你可以用 暂停它Ctrl-Z
,bg
然后用disown
它把它拉到后台,就像这样:
$ sleep 1000
^Z
[1]+ Stopped sleep 1000
$ bg
$ disown
$ exit
Run Code Online (Sandbox Code Playgroud)
ger*_*ijk 54
@StevenD 已经发布了很好的答案,但我认为这可能会澄清一点。
该进程在终端终止时被杀死的原因是您启动的进程是终端的子进程。关闭终端后,这也会杀死这些子进程。你可以看到进程树pstree
,例如kate &
在 Konsole 中运行时:
init-+
??konsole???bash???kate???2*[{kate}]
? ? ??pstree
? ??2*[{konsole}]
Run Code Online (Sandbox Code Playgroud)
要使kate
进程从konsole
您终止时分离,请与命令一起konsole
使用nohup
,如下所示:
nohup kate &
Run Code Online (Sandbox Code Playgroud)
关闭后konsole
,pstree
将如下所示:
init-+
|-kate---2*[{kate}]
Run Code Online (Sandbox Code Playgroud)
而kate
将生存。:)
一个替代方案是使用screen
/ tmux
/ byobu
,其将保持壳运行,独立于终端的。
小智 32
您可以在终端中运行这样的过程
setsid process
这将在新会话中运行该程序。如解释http://hanoo.org/index.php?article=run-program-in-new-session-linux
daG*_*aGo 13
我更喜欢:
(应用名称 &)
例如:
linux@linux-desktop:~$ (chromium-browser &)
确保在键入命令时使用括号!
小智 12
尽管所有建议都运行良好,但我发现我的替代方法是使用screen
,这是一个在屏幕上设置虚拟终端的程序。
您可以考虑从. Screen 几乎可以安装在所有 Linux 和 Unix 衍生产品上。点击+和(小写)将开始第二个会话。这将允许你通过点击来来回切换初始会话之间+和或较新的会话击中+和。您可以在一个终端中进行多达 10 个会话。我曾经在工作中开始一个会话,回家,ssh 进入我的工作机器,然后调用. 这将使您重新连接到该远程会话。screen -S session_name
CtrlACCtrlA0CtrlA1screen -d -R session_name
我有一个脚本:
我用它主要为gedit
,evince
,inkscape
等所有有很多烦人的终端输出的。如果命令在之前完成TIMEOUT
,则返回 nohup 的退出状态而不是零。
#!/bin/bash
TIMEOUT=0.1
#use nohup to run the command, suppressing its output and allowing the terminal to be closed
#also send nohup's output to /dev/null, supressing nohup.out
#run nohup in the background so this script doesn't block
nohup "${@}" >/dev/null 2>&1 &
NOHUP_PID=$!
#kill this script after a short time, exiting with success status - command is still running
#this is needed as there is no timeout argument for `wait` below
MY_PID=$$
trap "exit 0" SIGINT SIGTERM
sleep $TIMEOUT && kill $MY_PID 2>/dev/null & #ignore "No such process" error if this exits normally
#if the command finishes before the above timeout, everything may be just fine or there could have been an error
wait $NOHUP_PID
NOHUP_STATUS=$?
#print an error if there was any. most commonly, there was a typo in the command
[ $NOHUP_STATUS != 0 ] && echo "Error ${@}"
#return the exit status of nohup, whatever it was
exit $NOHUP_STATUS
Run Code Online (Sandbox Code Playgroud)
例子...
>>> run true && echo success || echo fail
success
>>> run false && echo success || echo fail
Error false
fail
>>> run sleep 1000 && echo success || echo fail
success
>>> run notfound && echo success || echo fail
Error notfound
fail
Run Code Online (Sandbox Code Playgroud)
执行所有这些操作的仅 shell 方法是关闭 stdin 和后台命令:
command <&- &
Run Code Online (Sandbox Code Playgroud)
那么当你退出shell时它不会退出。重定向标准输出是一个不错的可选操作。
缺点是事后你不能这样做。
小智 5
您可以将进程 (PID) 设置为在注销和关闭终端会话时不接收 HUP 信号。使用以下命令:
nohup -p PID
Run Code Online (Sandbox Code Playgroud)