{"id":98,"date":"2008-09-09T19:58:16","date_gmt":"2008-09-09T18:58:16","guid":{"rendered":"http:\/\/www.hlynes.com\/?p=98"},"modified":"2008-12-01T00:26:05","modified_gmt":"2008-11-30T23:26:05","slug":"dealing-with-stupid-programs-that-think-they-need-x","status":"publish","type":"post","link":"https:\/\/www.hlynes.com\/?p=98","title":{"rendered":"Dealing With Stupid Programs That Think They Need X"},"content":{"rendered":"<p>The new compute cluster is beginning to feel like a production system. I&#8217;m currently run off my feet installing software for the stream of new users. Mostly this is fine, but occasionally I run into software that makes me want to band my head repeatedly on my desk until the pain goes away; or more accurately makes me want to bang the programmer&#8217;s head on the desk.<\/p>\n<p>Just today we received a linux port of a code that has been running on the Windows Condor pool for a while now. Everything seemed fine except for it&#8217;s stubborn refusal to run if it couldn&#8217;t find a windowing system. Bear in mind that it doesn&#8217;t actually produce any graphical output it just dies if it can&#8217;t connect to X. After a bit of futzing around we discover that the people that normally run this code do something like:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\nXvfb :1 -server 1 1024x1024x8 &amp;\r\nexport DISPLAY=:1\r\n.\/stupid_code_that_wants_X\r\n<\/pre>\n<p>Xvfb is the X virtual framebuffer. It creates a running X client without actually needing any graphics to be running.<\/p>\n<p>Which works just great locally but if you want to launch that as a script in the job scheduling system (we use PBSpro) then you need to be a bit more careful. What happens if two of these jobs try to launch on the same machine? Obviously one of them will fail because display 1 is already allocated. What I really needed was a script that will try to launch Xvfb and increment DISPLAY on failure until it finds a display that is free. For your edification here it is:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\nget_xvfb_pid () {\r\n\tXVFB_PID=`ps -efww | grep -v grep | grep Xvfb |\\\r\n       grep $USERNAME | tail -n 1 | awk &#039;{print $2}&#039;`\r\n\t}\r\n\r\ncreate_xvfb () {\r\n\tUSERNAME=`whoami`\r\n\tDISPLAYNO=1\r\n\twhile &#x5B; -z $xvfb_success ]\r\n\t\tdo\r\n\t\tget_xvfb_pid\r\n\t\told_XVFB_PID=$XVFB_PID\r\n\t\tXVFB_PID=&quot;&quot;\r\n\t\tXvfb :${DISPLAYNO} -screen 0 1024x1024x8 &gt;&amp; \/dev\/null &amp;\r\n\t\tsleep 1\r\n\t\tget_xvfb_pid\r\n\t\tif ! &#x5B; -z $old_XVFB_PID ]\r\n\t\t\tthen\r\n\t\t\tif &#x5B; -z $XFVB_PID ] &amp;&amp; ! &#x5B; $XVFB_PID == $old_XVFB_PID ]\r\n\t\t\t\tthen\r\n\t\t\t\techo &quot;Started XVFB on display $DISPLAYNO process $XVFB_PID&quot;\r\n\t\t\t\txvfb_success=1\r\n\t\t\telse\r\n\t\t\t\tDISPLAYNO=$(($DISPLAYNO + 1))\r\n\t\t\t\tXVFB_PID=&quot;&quot;\r\n\t\t\tfi\r\n\t\telse\r\n\t\t\tif &#x5B; -z $XFVB_PID ]\r\n                                then\r\n                                echo &quot;Started XVFB on display $DISPLAYNO process $XVFB_PID&quot;\r\n                                xvfb_success=1\r\n                        else\r\n                                DISPLAYNO=$(($DISPLAYNO + 1))\r\n                                echo &quot;FAIL!&quot; $XVFB_PID\r\n                                XVFB_PID=&quot;&quot;\r\n                        fi\r\n\t\tfi\r\n \t\tdone\r\n\texport XVFB_PID\r\n\texport DISPLAY=:${DISPLAYNO}\r\n\t}\r\n\r\nkill_xvfb () {\r\n\tkill $XVFB_PID\r\n\t}\r\n<\/pre>\n<p>Which you can call from a script like thus:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\n&#x5B;arccacluster8]$. .\/xvfb_helper\r\n&#x5B;arccacluster8]$ create_xvfb\r\nStarted XVFB on display 1 process 9563\r\n&#x5B;arccacluster8 ~]$ echo $DISPLAY\r\n:1\r\n&#x5B;arccacluster8 ~]$ echo $XVFB_PID\r\n9563\r\n&#x5B;arccacluster8 ~]$ ps -efw | grep Xvfb\r\nusername    9563  9498  0 19:31 pts\/8    00:00:00 Xvfb :1 -screen 0 1024x1024x8\r\n&#x5B;arccacluster8 ~]$ kill_xvfb\r\n&#x5B;arccacluster8 ~]$ ps -efw | grep Xvfb\r\n&#x5B;arccacluster8 ~]$\r\n<\/pre>\n<p>I submit that this is a disgraceful hack, but it might come in handy to someone else.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The new compute cluster is beginning to feel like a production system. I&#8217;m currently run off my feet installing software for the stream of new users. Mostly this is fine, but occasionally I run into software that makes me want to band my head repeatedly on my desk until the pain goes away; or more &hellip; <a href=\"https:\/\/www.hlynes.com\/?p=98\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Dealing With Stupid Programs That Think They Need X<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[],"class_list":["post-98","post","type-post","status-publish","format-standard","hentry","category-sysadmin"],"_links":{"self":[{"href":"https:\/\/www.hlynes.com\/index.php?rest_route=\/wp\/v2\/posts\/98"}],"collection":[{"href":"https:\/\/www.hlynes.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.hlynes.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.hlynes.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.hlynes.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=98"}],"version-history":[{"count":10,"href":"https:\/\/www.hlynes.com\/index.php?rest_route=\/wp\/v2\/posts\/98\/revisions"}],"predecessor-version":[{"id":160,"href":"https:\/\/www.hlynes.com\/index.php?rest_route=\/wp\/v2\/posts\/98\/revisions\/160"}],"wp:attachment":[{"href":"https:\/\/www.hlynes.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=98"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.hlynes.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=98"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.hlynes.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=98"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}