Search code examples
bashmacosshellcommand-linecron

Simple bash script doesn't cron properly


I'm attempting to cron a simple bash script on my macbook-pro laptop. Ultimately, I would like to first get this to work for bash script and then move on to my python scripts. I've created a simple bash file (named hello.sh) with the code below:

#!/bin/bash

echo "Hello World" >> /Users/myusername/Desktop/test.txt

And my crontab -e is designated as follows:

* * * * * /bin/bash /Users/myusername/Desktop/bash-files/hello.sh

However, I get nothing after waiting a minute.

After googling around, I concluded that maybe I was running into the "gotcha" issue (cron reading different parameters than env). So I queued the following:

* * * * * env > /tmp/env.output

and it's output as follows

SHELL=/bin/sh
USER=myusername
PATH=/usr/bin:/bin
PWD=/Users/myusername
SHLVL=1
HOME=/Users/myusername
LOGNAME=myusername
_=/usr/bin/env

running env in my terminal produces the following relevant parameters:

SHELL=/bin/zsh
USER=myusername
PATH=/Users/myusername/opt/anaconda3/bin:/Users/myusername/opt/anaconda3/condabin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Library/TeX/texbin:/opt/X11/bin
PWD=/tmp
SHLVL=1
HOME=/Users/myusername
LOGNAME=myusername
_=/usr/bin/env

I've added the above parameter settings to my hello.sh script but I still get nothing.

Can anyone point out to what my issue is here?


Solution

  • These are permission and path errors, easily resolved.

    cron full disk access

    Look in system preferences to grant full disk access to your binaries and unset PATH in your scripts to catch any paths that are not complete.

    I recommend /usr/local/bin for ease of maintaining any scripts you wish to have launchd or cron to schedule.

    There’s no reason you can’t run from your user folder if that suits you, however.