击:在循环睡眠都有自己的PID [英] Bash: a sleep in a while loop gets its own pid

查看:155
本文介绍了击:在循环睡眠都有自己的PID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个bash脚本,它在一个循环中一些并行处理。我不希望并行处理秒杀CPU,所以我用一个睡眠命令。下面是一个简化版本。


(而真实的;做睡觉99999;完成)

所以,我从一个bash提示符下执行上面的线,并得到类似:
[1] 12345

其中, [1] 是作业号, 12345 是while循环的进程ID(PID) 。我做了杀12345 并获得:


[1] +终止(而真实;做
    睡眠99999;
完成)

它看起来像整个脚本被终止。但是,我做了的ps aux | grep的睡眠并找到睡眠命令依然强劲,但有自己的PID!我可以杀睡眠,一切似乎罚款。但是,如果我先杀掉睡眠,while循环开始一个新的睡眠 PID。这是这样一个让我吃惊,因为睡眠不平行,while循环。循环本身是一个单独的执行路径。

所以,我有两个问题:


  1. 为什么睡眠命令让自己的进程ID?

  2. 如何轻松杀掉while循环和睡眠?


解决方案

  1. 睡眠都有自己的 PID ,因为它是一个进程在运行,只是在等待。尝试其中睡眠来看看它在哪里。

  2. 您可以使用 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:

  1. Why did the sleep command get its own process ID?
  2. How do I easily kill the while loop and the sleep?

解决方案

  1. Sleep gets its own PID because it is a process running and just waiting. Try which sleep to see where it is.
  2. You can use ps -uf to see the process tree on your system. From there you can determine what the PPID (parent PID) of the shell (the one running the loop) of the sleep is.

这篇关于击:在循环睡眠都有自己的PID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆