The particular motive for the failure is that the setting in cron
is a special setting than “… in standard interactive shell”. If you wish to study how the setting is completely different in cron
vs your interactive shell
, attempt working printenv
(or env
) from every & observe the variations; i.e.:
% crontab -e
... crontab opens within the editor you've got chosen as default (an setting 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, examine the environments
listed within the two information:
cat $HOME/myshellenv.txt
...
cat $HOME/mycronenv.txt
...
See the distinction? Word particularly the distinction within the PATH
setting variable.
Subsequent, discover out the place your ping
command is situated:
whereis ping
ping: /sbin/ping /usr/share/man/man8/ping.8
Ask your self, “Is /sbin
included within the PATH setting 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 notice the addition of -c 1
has nothing to do with setting
, that’s merely to maintain you from clogging your system with a bunch of ping
processes!!
One different factor you could must do beneath macOS:
-
Open
System Settings
, then choosePrivateness and Safety
, after whichFull Disk Entry
-
Verify 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 offercron
“Full Disk Entry”)
And FWIW, see this recipe overlaying utilization of cron
& crontab