'shell command fails when running from crontab

[Edit: the original title of this question was "Applescript running from crontab not accessing network?" but having found that the problem is about PATHs--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.



Solution 1:[1]

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.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Nat Kuhn