Bash 等待进程启动 [英] Bash wait for process start

查看:17
本文介绍了Bash 等待进程启动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想对 Bash 说,等待进程开始/开始.例如,我正在尝试这种方式:

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

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

我有以下问题:

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

  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 个命令执行正确,则执行 1 次我怎么能继续做???

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

为此,最好使用直到或同时"???

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

wait Process Start or Begin 的另一种想法???

Another ideas for wait Process Start or Begin ???

推荐答案

您的代码存在多个问题:

There are multiple issues with your code:

  1. 如果没有进程,pidof 不会打印 0 或 -1,所以你的逻辑是错误的
  2. 如果有多个进程,pidof 可以返回多个 pid,这会破坏您的比较
  3. > 必须在 [ ] 中转义,否则您的代码等效于 [ $notepad ] >0,指向一个文件.
  4. > 甚至不是正确的运算符.您想要 -gt,但正如第 1 点和第 2 点所述,您不应该比较数字.
  5. until 运行循环,直到条件为真.它不会等待条件变为真,然后运行循环.
  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. until runs the loop until the condition is true. it doesn't wait for the condition to become true, and then run the loop.

你应该这样做:

# 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

这篇关于Bash 等待进程启动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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