无法在终端后台进行 运行 无限循环处理。
Unable to run infinite loop process in background in terminal.
随着 Season1 Episode8 Processes and Jobs 视频的播放,我尝试了这些命令。我在 Ubuntu 16.04 有一个 bash 终端机 运行。
while true; do echo ping; sleep 1; done
^Z
而不是得到:
[1]+ Stopped while true; do echo ping; sleep 1; done
我得到:
[1]+ Stopped sleep 1
bg%1
进一步只给出
[1]+ sleep 1 &
而不是在后台以 1s 间隔进行一系列 ping
任何关于为什么会发生这种情况以及如何在后台以 1s 间隔实际获得一系列 ping 的任何想法都将不胜感激。
运行 您的命令末尾带有 &,而不是停止它。 ^Z 太窄,无法与这样的命令一起使用。
您可以尝试这样的操作:
while true; do
echo ping 1
sleep 1
done;
请注意,我只在 done
上放置了分号 ;
- 标记语句的结尾。我在我的终端上试过了,结果如你所料。
您 运行 通过在末尾添加 & 来执行命令,这样更容易结束进程。
admin1@mysys:~$ while true; do echo ping; sleep 1; done&
[2] 14169
admin1@mysys:~$ ping
ping
ping
^C
admin1@mysys:~$ ping
ping
ping
^C
admin1@mysys:~$ ping
ping
ping
ping
^C
admin1@mysys:~$ ping
kill 14169
admin1@mysys:~$
如您所见,您必须按 Cntrl + D 或终止进程才能停止它。
另一种选择是使用 'screen'
假设你安装了screen,进入终端执行命令'screen'
然后可以执行命令:
while true; do echo ping; sleep 1; done
然后按 Cntrl A,然后按 D(保持 Cntrl 自己按下)。这将使你脱离屏幕,你可以做任何你想做的事,命令将在后台执行。
任何时候你都可以列出当前正在执行的屏幕
screen -ls
然后通过执行
连接回屏幕
screen -r screen_name
这听起来有点复杂,但却是一种更好的处理方式。您可以找到更多详细信息 Here
向您的进程借用 [ this ] answer, Ctrl-Z generates the [ TSTP ] 信号并停止该进程显然不是您的意图。
要运行一个后台进程,执行
process >/dev/null &
# Here the '>/dev/null' suppresses any output from the command
# popping up in the screen, this may or may not be desirable
# The & at the end tells bash that the command is to be run in backgroud
例如
$ ping -c 100 192.168.0.1 >/dev/null &
[1] 2849
注意 bash 给你的两个数字 [1]
和 2849
。第一个是后台进程号。比如说,如果你想把这个过程带到前台,你可以使用这个数字
fg 1 # Here fg stands for foreground
第二个数字是进程ID,即2849
。说,你想终止
过程,你可以像下面那样做:
kill -9 2849 #-9 is for SIGKILL
编辑
在你的情况下,你可以将循环包装在如下函数中
while_fun() {
while true
do
echo "PING"
done
}
并做
while_fun >dev/null &
或者做
while true
do
echo "PING"
done >/dev/null &
尝试:
bash <<< 'while true; do echo ping; sleep 1; done'
结果:
^Z
[1]+ Stopped bash <<< 'while true; do echo ping; sleep 1; done'
或使用子外壳:
(while true; do echo ping; sleep 1; done)
结果:
^Z
[1]+ Stopped ( while true; do
echo ping; sleep 1;
done )
随着 Season1 Episode8 Processes and Jobs 视频的播放,我尝试了这些命令。我在 Ubuntu 16.04 有一个 bash 终端机 运行。
while true; do echo ping; sleep 1; done
^Z
而不是得到:
[1]+ Stopped while true; do echo ping; sleep 1; done
我得到:
[1]+ Stopped sleep 1
bg%1
进一步只给出
[1]+ sleep 1 &
而不是在后台以 1s 间隔进行一系列 ping
任何关于为什么会发生这种情况以及如何在后台以 1s 间隔实际获得一系列 ping 的任何想法都将不胜感激。
运行 您的命令末尾带有 &,而不是停止它。 ^Z 太窄,无法与这样的命令一起使用。
您可以尝试这样的操作:
while true; do
echo ping 1
sleep 1
done;
请注意,我只在 done
上放置了分号 ;
- 标记语句的结尾。我在我的终端上试过了,结果如你所料。
您 运行 通过在末尾添加 & 来执行命令,这样更容易结束进程。
admin1@mysys:~$ while true; do echo ping; sleep 1; done&
[2] 14169
admin1@mysys:~$ ping
ping
ping
^C
admin1@mysys:~$ ping
ping
ping
^C
admin1@mysys:~$ ping
ping
ping
ping
^C
admin1@mysys:~$ ping
kill 14169
admin1@mysys:~$
如您所见,您必须按 Cntrl + D 或终止进程才能停止它。
另一种选择是使用 'screen'
假设你安装了screen,进入终端执行命令'screen'
然后可以执行命令:
while true; do echo ping; sleep 1; done
然后按 Cntrl A,然后按 D(保持 Cntrl 自己按下)。这将使你脱离屏幕,你可以做任何你想做的事,命令将在后台执行。
任何时候你都可以列出当前正在执行的屏幕
screen -ls
然后通过执行
连接回屏幕screen -r screen_name
这听起来有点复杂,但却是一种更好的处理方式。您可以找到更多详细信息 Here
向您的进程借用 [ this ] answer, Ctrl-Z generates the [ TSTP ] 信号并停止该进程显然不是您的意图。
要运行一个后台进程,执行
process >/dev/null &
# Here the '>/dev/null' suppresses any output from the command
# popping up in the screen, this may or may not be desirable
# The & at the end tells bash that the command is to be run in backgroud
例如
$ ping -c 100 192.168.0.1 >/dev/null &
[1] 2849
注意 bash 给你的两个数字 [1]
和 2849
。第一个是后台进程号。比如说,如果你想把这个过程带到前台,你可以使用这个数字
fg 1 # Here fg stands for foreground
第二个数字是进程ID,即2849
。说,你想终止
过程,你可以像下面那样做:
kill -9 2849 #-9 is for SIGKILL
编辑
在你的情况下,你可以将循环包装在如下函数中
while_fun() {
while true
do
echo "PING"
done
}
并做
while_fun >dev/null &
或者做
while true
do
echo "PING"
done >/dev/null &
尝试:
bash <<< 'while true; do echo ping; sleep 1; done'
结果:
^Z
[1]+ Stopped bash <<< 'while true; do echo ping; sleep 1; done'
或使用子外壳:
(while true; do echo ping; sleep 1; done)
结果:
^Z
[1]+ Stopped ( while true; do
echo ping; sleep 1;
done )