[autotest] Take a screenshot when a test fails.

If X is not running, no screenshot will be taken.

BUG=chromium:383605
TEST=test_that 172.22.30.146 dummy_Fail
confirm screenshot is created, e.g.,
/tmp/test_that_results_rQiKvU/results-1-dummy_Fail/dummy_Fail.Fail/debug/dummy_Fail.Fail-fail-0.png

Change-Id: Ic044f611ab6f5d65d8ca973f9847346b3599891c
Reviewed-on: https://chromium-review.googlesource.com/204689
Commit-Queue: Dan Shi <dshi@chromium.org>
Tested-by: Dan Shi <dshi@chromium.org>
Reviewed-by: Dan Shi <dshi@chromium.org>
diff --git a/client/common_lib/site_utils.py b/client/common_lib/site_utils.py
index 2619410..64f1e92 100644
--- a/client/common_lib/site_utils.py
+++ b/client/common_lib/site_utils.py
@@ -319,6 +319,9 @@
     @returns complete path to saved screenshot file.
 
     """
+    if not _is_x_running():
+        return
+
     next_index = len(glob.glob(
         os.path.join(dest_dir, '%s-*.%s' % (fname_prefix, format))))
     screenshot_file = os.path.join(
@@ -381,4 +384,11 @@
         # another exception.
         if old_exc_type is None:
             raise
-        logging.error(err)
\ No newline at end of file
+        logging.error(err)
+
+
+def _is_x_running():
+    try:
+        return int(base_utils.system_output('pgrep -o ^X$')) > 0
+    except Exception:
+        return False