【问题标题】:Script working via terminal but not with cron脚本通过终端工作,但不适用于 cron
【发布时间】:2017-08-01 20:25:24
【问题描述】:

这个测试脚本正是复制我的问题的场景。

import atomac, time

noront = atomac.getAppRefByBundleId(BUNDLE_ID)
noront.activate()
time.sleep(5)
ax = {'AXRole' : 'AXButton'}
toggle = noront.findAllR(**ax)
print "Toggle is ", toggle
with open("/tmp/test1.txt", 'w') as fp:
    fp.write(str(toggle))

当我在终端中运行这个脚本时,我得到了输出

python /Users/user/Desktop/test.py

当我跑步后 猫 /tmp/test1.txt

[<atomac.AXClasses.NativeUIElement AXButton u'I agree'>, <atomac.AXClasses.NativeUIElement AXButton u'I Accept'>, <atomac.AXClasses.NativeUIElement AXButton u'Pr P'>, <atomac.AXClasses.NativeUIElement AXButton None>, <atomac.AXClasses.NativeUIElement AXButton None>, <atomac.AXClasses.NativeUIElement AXButton None>]

但是,我为此添加了一个 cron 条目。

* * * * * sleep 10; python /Users/user/Desktop/test.py

当我跑步后

cat /tmp/test1.txt
[]

它返回空集。有人可以帮我为什么当我通过 cron 运行时没有检测到元素吗?

和权限有关吗?

【问题讨论】:

  • 向脚本添加一些日志记录可能会很有用,以便查看发生了什么问题。您可以将此行添加到 cron 定义中:* * * * * sleep 10; python /Users/user/Desktop/test.py &gt;&gt; /tmp/cron.log
  • 是的。我试着添加那个。我得到“切换是[]”。我确信 cron 正在触发。但这里的问题是,为什么它在通过 cron 运行时没有检测到 ui 元素?

标签: python cron


【解决方案1】:

我认为您每秒都在运行您的 cron 作业。您可以检查/tmp/test1.txt 的日期以确保它一直在重新创建。

我还认为您还应该在之前加载.profile,如下所示:

30 14 * * * . .profile; python /Users/user/Desktop/test.py

【讨论】:

  • 我确信脚本正在执行。但不确定为什么它在通过 cron 运行时没有检测到 UI 元素
  • 而且我没有 bash 配置文件。您建议在 bash 个人资料中保留什么?
猜你喜欢
  • 1970-01-01
  • 2018-02-03
  • 1970-01-01
  • 2018-05-27
  • 2017-08-05
  • 2011-05-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多