【问题标题】:Running mac osx commands from cronjob从 cronjob 运行 mac osx 命令
【发布时间】:2016-04-24 08:55:38
【问题描述】:

我的 macbook 中有一个使用 mac osx El Capitan 的 cronjob(每 10 分钟截屏一次),它执行以下命令:

*/10 * * * * cd /Users/userx/Pictures/memes && ./shared.sh

shared.sh 脚本包含以下内容:

#!/bin/bash
_now=$(date +"%m_%d_%Y_%H_%M_%S")
screencapture -x $_now.png

但是当系统执行 cronjob 我得到这个错误:

./shared.sh: line 3: screencapture: command not found

但如果我直接在终端中运行 shared.sh 文件,一切都会正确执行。

有什么想法吗??我们可以从 cronjobs 运行 mac osx 命令吗??

【问题讨论】:

    标签: macos unix terminal crontab


    【解决方案1】:

    screencapture 在 /usr/sbin 中,但 cron 作业的默认 PATH 只是 /usr/bin:/bin,所以找不到。有几种方法可以解决这个问题:

    • 在脚本中使用完整路径(即/usr/sbin/screencapture -x $_now.png)。
    • 在使用screencapture(即PATH=/usr/bin:/bin:/usr/sbin:/sbin)之前在脚本中设置PATH。
    • 在脚本条目之前在 crontab 中设置 PATH(语法与脚本中的相同)。

    【讨论】:

    • 谢谢!,我使用/usr/sbin/screencapture -x $_now.png 并且工作正常:)
    猜你喜欢
    • 2012-08-09
    • 2018-04-08
    • 2015-10-27
    • 2015-04-09
    • 2010-11-19
    • 1970-01-01
    • 2014-12-29
    • 2012-04-12
    • 2015-04-19
    相关资源
    最近更新 更多