Recursive while loop in docker cmd

2019-09-18 02:27发布

问题:

I'm trying to run google chrome inside docker containers. I have been able to successfully do so, however there have been instances where chrome would not run on some of the containers (mass creation of containers).

So I'm looking to run a while loop till the chrome process is found to be running. I've tried the following but with errors like "bash: [if: command not found"

    var chrome_command = 'google-chrome --user-data-dir=/home/ubuntu/chrome-user-dir';
var cmd = ''; cmd += 'Xvfb :99 & '; cmd += 'export DISPLAY=:99 & '; cmd += 'x11vnc -rfbport 6001 -display :99 -bg -q & '; cmd += 'while [if ps aux | grep "google-chrome"|grep -v grep > /dev/null]; do ' + chrome_command + '; sleep 1 ; done';

Could someone point out where I am going wrong! Thanks!

回答1:

The last line should be:

    cmd += 'while ! ps aux | grep -v grep | grep -q chrome; do ' + chrome_command + '; sleep 1 ; done';
  • -q for the last grep would suffice to see if there's matched lines in the input.
  • Use ! to negate the result of list of commands.
  • It seems google-chrome won't appears in ps aux.