擅长:python、mysql、java
<p>除了<code>$DISPLAY</code>之外,您可能还需要设置<code>$DBUS_SESSION_BUS_ADDRESS</code>环境,因为gsettings正在使用dbus(在未设置DISPLAY时发现,错误消息表示无法自动启动D-Bus);您可以尝试:</p>
<pre><code>DISPLAY=:0
*/5 * * * * eval $(ps -ww -p $(pgrep gnome-session) -o cmd= e | fmt -1 | grep DBUS_SESSION_BUS_ADDRESS) python3 /usr/local/cronwal/cronwal.py
</code></pre>
<p>这将从crontab调用程序拥有的<code>gnome-session</code>进程的环境中获取会话地址,并将其设置在python3进程的环境中,从而允许forked <code>gsettings</code>命令通过会话总线进行通信</p>
<p>您可以使用<code>printenv</code>检查终端中是否设置了DBUS_SESSION_BUS_ADDRESS,如果上面有语法错误(我无法测试),则复制该地址,但它将无法在下一个gnome会话上工作。当然,如果您这样硬编码它,则每次都必须更改它:</p>
<pre><code>DISPLAY=:0
*/5 * * * * DBUS_SESSION_BUS_ADDRESS="whatever printenv says" python3 /usr/local/cronwal/cronwal.py
</code></pre>