Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
783 views
in Technique[技术] by (71.8m points)

loops - Bash wait for process start

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...