Bash wait for the process to begin

I'm trying to tell Bash, wait for the process to start / start. I try 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 

Ok, I have the following questions:

  • why it will generate a file with the name "0" (the file is empty) I do not want to create a new file, just wait until the PID checks the execution.

  • This is a Loop, but if 2 teams perform correlation, 1 time how can I continue to do ???

  • To do this, it is better to use "before or during"

  • Other ideas for waiting. Starting a process or getting started.

+6
source share
3 answers

There are several problems in the code:

  • pidof does not print 0 or -1 if there is no process, so your logic is incorrect.
  • pidof can return multiple pids if there are multiple processes, which will break your comparison.
  • > must be escaped to [ ] , otherwise your code will be equivalent to [ $notepad ] > 0 , directing the file.
  • > not even the right operator. You wanted -gt , but as mentioned in points 1 and 2, you should not compare numbers.
  • until starts the loop until the condition is true. it does not wait for the condition to become true, and then start the loop.

Here's 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 
+14
source

To answer your first question: '>' is not a math / comparison operator 'more than'; this is a bash way to let you output to a file (handle).

 echo "some text" > myfile.txt 

will create a file called myfile.txt, and '>' will send the output of the command to this file. I would suggest that your file "0" has a pid, and nothing more.

Instead, try -gt (or related options: -ge , -lt , -le , -eg , -ne ) to check if one value is greater (or: greater than or equal, less, less or equal, equal, not equal , respectively) to see if this helps. I.e

 until [ $notepad -gt 0 ] 
+2
source

If I understand correctly, you want to wait until notepad starts. Then pidof should be inside your loop.

 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 
+1
source

Source: https://habr.com/ru/post/955804/


All Articles