Python Get Mouse position in a cron script
<p>I am using python3 and I am able to get the mouse position using the following codes in Centos 7:</p>

<pre><code>from Xlib import display
data = display.Display().screen().root.query_pointer()._data
data["root_x"], data["root_y"]
</code></pre>

<p>However, when I run the script using crontab, it shows the following exception:
Xlib.error.DisplayNameError: Bad display name ""</p>

<p>Is there a way I can get the mouse position using python and cron job?</p>

<p>It is because the environment variable <code>DISPLAY</code> is not set properly during execution of your python script.  Try adding the following line to the beginning of your python script, for example:</p>

<pre><code>import os
os.environ['DISPLAY'] = ':0.0'  # the value should match your setting
</code></pre>

python cron