Bash的等待过程中启动 [英] Bash wait for process start

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

问题描述

我想对你说猛砸,等待一个进程启动/开始。我通过这样的方式试图例如:

 记事本=`的pidof notepad.exe`
    直到[$记事本> 0]
        做
            包括taskset -p 03 $记事本
            使用renice -n 5 -p $记事本
            睡眠5
            使用renice -n 0 -p $记事本
        DONE

好吧,我有后续的问题:


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


  2. 这是一个循环,但如果2命令执行correclty,1次
     我怎么能继续做过???


  3. 有关本公司更好的利用,直到或者在???


  4. 另外一个想法,等待过程启动或开始???



解决方案

有您的code多的问题:


  1. 的pidof不打印0或-1,如果没有过程,所以你的逻辑是错误的

  2. 的pidof可以返回多个的PID,如果有多个进程,这将打破你的比较

  3. > 在转义 [] ,否则你的code等同于 [$记事本> 0 ,定向到一个文件中。

  4. > 甚至不是正确的运营商。你想 -gt ,但在点#1和#2所提到的,你不应该比较数字。

  5. 直到,直到条件为真运行循环。它不会等待条件成为真正的,然后运行循环。

这是你应该怎么做:

 #等待记事本开始
直到的PID = $(的pidof记事本)

    睡眠1
DONE#记事本现在已经开始。#可能有多个记事本程序,所以在循环的PID
在$ PID的PID

    包括taskset -p 03 $ PID
    renice只能-n 5 -p $ PID
    睡眠5
    renice只能-n 0 -p $ PID
DONE#记事本程序(ES)现在已经被处理

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

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 ???

解决方案

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. until runs the loop until the condition is true. it doesn't wait for the condition to become true, and then run the loop.

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

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

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