No protocol specified

I ran into weird problems running some X utilities such as xset or xdpyinfo through an XMLRPC server. After some debugging I kept getting a “No protocol specified” error. It turned out that many X related tools require the HOME environment variable to be set, and the XMLRPC server environment was missing it. The solution has been calling the tools this way, prefixing the env command:

env HOME=/root xset -d 0:0 q

BTW: a useful way to debug external commands ran through ruby is with the popen3 function. It works this way:

require 'open3'
cmd = "ls -l"
stdin, stdout, stderr = Open3.popen3(cmd)
out_line = stdout.gets
err_line = stderr.gets