Bash 等待进程启动

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19326245/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 08:16:31  来源:igfitidea点击:

Bash wait for process start

bashloopsconditional-statements

提问by inukaze

I'm trying to say to Bash , wait for a Process Start / Begin. I'm trying by this way for example:

我试图对 Bash 说,等待进程开始/开始。例如,我正在尝试通过这种方式:

notepad=`pidof notepad.exe`
    until [ $notepad > 0 ]
        do
            taskset -p 03 $notepad
            renice -n 5 -p $notepad
            sleep 5
            renice -n 0 -p $notepad
        done

well i have the follow questions:

我有以下问题:

  1. why this generate a file called “0″ (the file are empty) i dont wanna make a new file , just wait for the PID to check execution.

  2. This is a Loop , but if the 2 commands are execute correclty , 1 time how i can continue to done ???

  3. For this its better use "until or while" ???

  4. Another ideas for wait Process Start or Begin ???

  1. 为什么这会生成一个名为“0”的文件(文件为空)我不想创建一个新文件,只需等待 PID 检查执行。

  2. 这是一个循环,但如果这 2 个命令正确执行,我如何继续执行 1 次???

  3. 为此,它更好地使用“直到或同时”???

  4. 等待进程开始或开始的另一个想法???

回答by that other guy

There are multiple issues with your code:

您的代码存在多个问题:

  1. pidof doesn't print 0 or -1 if there's no process, so your logic is wrong
  2. pidof can return multiple pids if there are multiple processes, which would break your comparison
  3. >has to be escaped in [ ]otherwise your code is equivalent to [ $notepad ] > 0, directing to a file.
  4. >isn't even the right operator. You wanted -gt, but as mentioned in point #1 and #2, you shouldn't compare numbers.
  5. untilruns the loop until the condition is true. it doesn't wait for the condition to become true, and then run the loop.
  1. 如果没有进程,pidof 不会打印 0 或 -1,所以你的逻辑是错误的
  2. 如果有多个进程,pidof 可以返回多个 pid,这会破坏您的比较
  3. >必须转义,[ ]否则您的代码等效于[ $notepad ] > 0, 指向文件。
  4. >甚至不是正确的操作员。您想要-gt,但如第 1 点和第 2 点所述,您不应该比较数字。
  5. until运行循环直到条件为真。它不会等待条件变为真,然后运行循环。

This is how you should do it:

你应该这样做:

# Wait for notepad to start
until pids=$(pidof notepad)
do   
    sleep 1
done

# Notepad has now started.

# There could be multiple notepad processes, so loop over the pids
for pid in $pids
do        
    taskset -p 03 $pid
    renice -n 5 -p $pid
    sleep 5
    renice -n 0 -p $pid
done

# The notepad process(es) have now been handled

回答by dwanderson

To answer your first question: '>' is not the mathematical/comparison operator 'greater than'; it's bash's way of letting you pipe output to a file (handle).

回答你的第一个问题:“>”不是数学/比较运算符“大于”;这是 bash 让您将输出通过管道传输到文件(句柄)的方式。

echo "some text" > myfile.txt

will create a file named myfile.txt, and '>' sent the output of the command into that file. I would imagine that your file '0' has the pid in it, and nothing else.

将创建一个名为 myfile.txt 的文件,并且 '>' 将命令的输出发送到该文件中。我想你的文件 '0' 里面有 pid,没有别的。

Instead, try -gt(or related variants: -ge, -lt, -le, -eg, -ne) to test if one value is greater than (or: greater than or equal, less than, less than or equal, equal, not equal, respectively) to see if that helps. That is,

相反,尝试-gt(或相关变体:-ge, -lt, -le, -eg, -ne)测试一个值是否大于(或:分别大于或等于、小于、小于或等于、等于、不等于)以查看是否有帮助。那是,

until [ $notepad -gt 0 ]

回答by damienfrancois

If I understand correctly, you want to wait until notepad is launched. Then the pidof must be inside your loop.

如果我理解正确,您要等到记事本启动。然后 pidof 必须在您的循环内。

while ! pidof notepad.exe >> /dev/null ;
do
sleep 1
done
notepad=$(pidof notepad.exe)
taskset -p 03 $notepad
renice -n 5 -p $notepad
sleep 5
renice -n 0 -p $notepad