
Once I execute ping in traditional interactive shell by hand, It succeeds. Why does it fail from cron ?
The particular purpose for the failure is that the atmosphere in cron
is a special atmosphere than “… in traditional interactive shell”. If you wish to be taught how the atmosphere is completely different in cron
vs your interactive shell
, attempt operating printenv
(or env
) from every & observe the variations; i.e.:
% crontab -e
... crontab opens within the editor you have chosen as default (an atmosphere variable!!)
... add following line to your crontab:
* * * * * /usr/bin/printenv > $HOME/mycronenv.txt 2>&1
... save & exit the editor
Now, out of your interactive shell
, enter the next command:
% /usr/bin/printenv > $HOME/myshellenv.txt 2>&1
Subsequent, evaluate the environments
listed within the two information:
cat $HOME/myshellenv.txt
...
cat $HOME/mycronenv.txt
...
See the distinction? Be aware particularly the distinction within the PATH
atmosphere variable.
Subsequent, discover out the place your ping
command is positioned:
whereis ping
ping: /sbin/ping /usr/share/man/man8/ping.8
Ask your self, “Is /sbin
included within the PATH atmosphere variable for cron
?”
The reply is: “No”
What ought to I do about that?
Most likely the best factor to do is change a line in your script:
FROM:
ping localhost
TO:
/sbin/ping -c 1 localhost
Please word the addition of -c 1
has nothing to do with atmosphere
, that’s merely to maintain you from clogging your system with a bunch of ping
processes!!
One different factor you could have to do beneath macOS:
-
Open
System Settings
, then choosePrivateness and Safety
, after whichFull Disk Entry
-
Examine to confirm that
cron
is listed (and enabled) for “Full Disk Entry” -
IF IT IS LISTED, BUT NOT ENABLED: Click on the toggle to allow it
-
IF IT IS NOT LISTED: Click on the
+
on the backside of the itemizing -
Add
cron
(beneath/usr/sbin/cron
to presentcron
“Full Disk Entry”)
And FWIW, see this recipe masking utilization of cron
& crontab