Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I'm having some issues with my CSH script and the command:

set XVFBCHK2 = `pgre -f 'Xvfb' | wc -l`

It seems to return inconsistent values. When it should return 1 it returns 0. I'm pretty sure that there is a the grep function is too fast and, therefore, does not catch the new instance of Xvfb following the command line:

Xvfb :1 -screen 0 1600x1200x16 >& /dev/null &

For instance, in this stretch of CSH script:

if ( $XVFBCHK ==  0 && $FIJICHK == 0 ) then
    set DISP=0
    set SCREEN=0
    Xvfb :1 -screen 0 1600x1200x16 >& /dev/null &

    set XVFBCHK2 = `pgrep -f 'Xvfb' | wc -l`
    echo $XVFBCHK2
    if ( $XVFBCHK2 == 1 ) then
            set DISP=1
            set SCREEN=0
    else if  ( $XVFBCHK2 == 0 ) then
            Xvfb :2 -screen 0 1600x1200x16 >& /dev/null &
            set DISP=2
            set SCREEN=0
    endif

    set XVFBCHK2 = `pgrep -f 'Xvfb' | wc -l`
    echo $XVFBCHK2
    if ( $XVFBCHK2 == 0 ) then
            Xvfb :3 -screen 0 1600x1200x16 >& /dev/null &
            set DISP=3
            set SCREEN=0
    endif

    set XVFBCHK2 = `pgrep -f 'Xvfb' | wc -l`
    echo $XVFBCHK2
    if ( $XVFBCHK2 == 0 ) then
            Xvfb :4 -screen 0 1600x1200x16 >& /dev/null &
            set DISP=4
            set SCREEN=0
    endif

    set XVFBCHK2 = `pgrep -f 'Xvfb' | wc -l`
    echo $XVFBCHK2
    if ( $XVFBCHK2 == 0 ) then
            Xvfb :5 -screen 0 1600x1200x16 >& /dev/null &
            set DISP=5
            set SCREEN=0
    endif

    set XVFBCHK2 = `pgrep -f 'Xvfb' | wc -l`
    echo $XVFBCHK2
    setenv DISPLAY :$DISP.$SCREEN
  elseif
  echo $XVFBCHK2
  echo $DISP

I can get the following output:

       0
       0
       3
       3
       3
       3
       3

So it seems that Xvfb was executed three times, but pgrep did not catch the executions until the third one. How do I make sure it catches the first try or accurately determines that the first try Xvfb :1 failed and it should try Xvfb :2 next?

share|improve this question

2 Answers 2

Here's a workaround that could work, depending on your implementation of X11 and possibly your platform.

On Linux with Xorg, you'll get a file (socket actually) under /tmp/.X11-unix for each display available on localhost. e.g. I have one Xvfb on :2 and a normal X on :0 and I get:

$ ls -l /tmp/.X11-unix
total 0
srwxrwxrwx 1 root root 0 Oct  4 09:06 X0
srwxrwxrwx 1 root root 0 Oct  4 11:37 X2

So rather than attempting to start your Xvfbs blindly, check whether the display is available with:

if ( -e /tmp/.X11-unix/X$DISP ) then
   ...
endif

You could use a loop to find the first available display number.

It's a bit racy, but should work under most circumstances. Not sure if this works (or if there are similar workarounds) on other operating systems.

share|improve this answer

so what i ended up doing and seems to work is to use a while loop for the pgrep so that I make sure it catches a new process:

    set i=0
    while ( $i <= 100 )
            set XVFBCHK2 = `pgrep -f 'Xvfb' | wc -l`
            @ i++
    end

Thank you for your answer Mat. I think that will work great as an alternative to search for the display not being used. I have another script for this purpose and your method is great for this purpose.

Turns out that our cluster administrator wants no more than one Xvfb display per node. As long as I first search that there are no displays running then I don't need to worry about finding a display not being used. I just need to worry about making sure that I was successful in executing an Xvfb display.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.