I have successfully run a crontab but when I introduce a python script, nothing happens. I have verified that my python file works when run directly from the shell.
crontab is:
* * * * * /Users/myname/myzsh.zsh
where myzsh.zsh is:
#!/bin/zsh
/Users/mypath_to/python /Users/myname/testscript.py
Nothing happens
CodePudding user response:
This is a common enough cron
gotcha.
Your environment is different between the interactive run and the cron run. This causes the observed failure.
I assume you showed us crontab -l
run as the same user
that ran the script interactively,
so we do not e.g. have a discrepancy with root
running it under cron.
Your $PWD
, present working directory, may matter.
Many cron jobs will do something like this:
cd some_dir && ./my_script.zsh
Quoting from the chapter 5 crontab man page:
... HOME, SHELL, and PATH may be overridden by settings in the crontab ...
... the environment handed to child processes is basically the one from /etc/rc.
You really want to embellish the environment before executing the script.
At a minimum I suspect you will need
env PYTHONPATH=. /Users/myname/myzsh.zsh
Many of my scripts set up a conda environment
before invoking cPython,
so $PATH
is good.
Run a cron job of simply env | sort >> /tmp/log.txt
to see what PATH looks like by default.
Try using that PATH setting interactively and
likely you will notice the script fails the same as it does under cron.
You may find it convenient to bury cd dir
and export PATH=...
within the myzsh.zsh script.
Consider running python -m site
to help debug sys.path
details.
Your script may be bailing out with a fatal error, a non-zero exit status. As an aid to debugging, consider adding
[email protected]
and then the cron
daemon will send you the script's output,
if it produces more than zero lines on stdout stderr.
CodePudding user response:
Turns out cron itself had no permissions. I dragged and dropped it into the security section of preferences and voila!