击:在循环睡眠都有自己的PID [英] Bash: a sleep in a while loop gets its own pid
问题描述
我有一个bash脚本,它在一个循环中一些并行处理。我不希望并行处理秒杀CPU,所以我用一个睡眠命令。下面是一个简化版本。
(而真实的;做睡觉99999;完成)
所以,我从一个bash提示符下执行上面的线,并得到类似: [1] 12345
其中, [1]
是作业号, 12345
是while循环的进程ID(PID) 。我做了杀12345
并获得:
[1] +终止(而真实;做
睡眠99999;
完成)
它看起来像整个脚本被终止。但是,我做了的ps aux | grep的睡眠
并找到睡眠命令依然强劲,但有自己的PID!我可以杀睡眠
,一切似乎罚款。但是,如果我先杀掉睡眠,while循环开始一个新的睡眠
PID。这是这样一个让我吃惊,因为睡眠不平行,while循环。循环本身是一个单独的执行路径。
所以,我有两个问题:
- 为什么睡眠命令让自己的进程ID?
- 如何轻松杀掉while循环和睡眠?
- 睡眠都有自己的
PID
,因为它是一个进程在运行,只是在等待。尝试其中睡眠
来看看它在哪里。 - 您可以使用
PS -uf
来看看你的系统进程树。从那里,你可以决定的外壳PPID
(父PID)(运行循环)的睡眠是什么。
I have a bash script that does some parallel processing in a loop. I don't want the parallel process to spike the CPU, so I use a sleep command. Here's a simplified version.
(while true;do sleep 99999;done)&
So I execute the above line from a bash prompt and get something like:
[1] 12345
Where [1]
is the job number and 12345
is the process ID (pid) of the while loop. I do a kill 12345
and get:
[1]+ Terminated ( while true; do sleep 99999; done )
It looks like the entire script was terminated. However, I do a ps aux|grep sleep
and find the sleep command is still going strong but with its own pid! I can kill the sleep
and everything seems fine. However, if I were to kill the sleep first, the while loop starts a new sleep
pid. This is such a surprise to me since the sleep is not parallel to the while loop. The loop itself is a single path of execution.
So I have two questions:
- Why did the sleep command get its own process ID?
- How do I easily kill the while loop and the sleep?
- Sleep gets its own
PID
because it is a process running and just waiting. Trywhich sleep
to see where it is. - You can use
ps -uf
to see the process tree on your system. From there you can determine what thePPID
(parent PID) of the shell (the one running the loop) of the sleep is.
这篇关于击:在循环睡眠都有自己的PID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!