从 crontab 运行时 shell 命令失败
[编辑:这个问题的原始标题是“Applescript running from crontab not accessing network?”但发现问题是关于 PATH
s - 不是网络,或者就 Applescript 而言 - 我试图使其更易于搜索。]
我有一个脚本,我想在以下位置运行每天晚上 9 点,如果我地下室的 Raspberry Pi 没有响应,就会向我发出警报。该脚本工作正常,并在我手动运行它时告诉我我的设备已启动:
osascript /users/nat/Code/applescript/ping-pi.scpt
它也从我的 crontab 运行,但它告诉我我的设备已关闭...但事实并非如此。
这是几分钟前运行的 crontab 行(用于测试,不是晚上 9 点):
14 15 * * * osascript /users/nat/Code/applescript/ping-pi.scpt
这是script:
--based on https://discussions.apple.com/thread/3833490
try
set ping to (do shell script "ping -c 2 <ddns site name here>")
display dialog "<ddns site name here> is up and running!" with icon note buttons {"OK"} default button 1
on error
-- if we get here, the ping failed
display dialog "<ddns site name here> is not responding" with icon caution buttons {"OK"} default button 1
end try
我唯一能想到的是,也许我需要以 root 身份运行它,但我不知道为什么会这样。
我也许可以摆脱“设置 ping 到”,但我只是调整我找到的代码。
[Edit: the original title of this question was "Applescript running from crontab not accessing network?" but having found that the problem is about PATH
s--not networking, or for that matter Applescript--I have tried to make it more searchable.]
I have a script which I would like to run at 9pm every night that will give me an alert if the Raspberry Pi in my basement is not responding. The script works fine, and tells me my device is up when I run it by hand:
osascript /users/nat/Code/applescript/ping-pi.scpt
It also runs from my crontab, but it tells me my device is down... which it isn't.
Here is the crontab line that worked a few minutes ago (for testing, not for 9pm):
14 15 * * * osascript /users/nat/Code/applescript/ping-pi.scpt
Here is the script:
--based on https://discussions.apple.com/thread/3833490
try
set ping to (do shell script "ping -c 2 <ddns site name here>")
display dialog "<ddns site name here> is up and running!" with icon note buttons {"OK"} default button 1
on error
-- if we get here, the ping failed
display dialog "<ddns site name here> is not responding" with icon caution buttons {"OK"} default button 1
end try
The only thing I can think of is that maybe I need to run it as root, but I don't know why that would be.
I can probably get rid of the "set ping to," but I'm just adapting code I found.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
事实证明,这根本不是网络访问问题,而是
PATH
问题。我在这篇文章中了解到 cron 作业的默认路径仅包含/bin
和/usr/bin
。which ping
告诉我我需要/sbin/ping
,当我相应地编辑脚本时,它起作用了。我想这会导致许多 cron 任务失败,所以我有点惊讶它没有更早引起我的注意。
正如我上面提到的,从 cron 作业中建立一个对话框是没有问题的,我将尝试删除我添加的额外权限,因为我猜我实际上不需要任何特殊的网络权限。
It turned out that this was not a network-access issue at all, but a
PATH
issue. I learned in this post that the default path for cron jobs includes just/bin
and/usr/bin
.which ping
told me that I needed/sbin/ping
, and when I edited the script accordingly, it worked.I would imagine that this would cause many cron tasks to fail, so I'm a little surprised that it didn't come to my attention sooner.
As I mentioned above, there is no problem putting up a dialog from a cron job, and I will try removing the extra permissions I added, because I'm guessing I don't actually need any special network permissions.