TIL: How to debug cron jobs
Today I learned how to properly debug cron jobs, thanks to https://serverfault.com/a/85906.
They key is to recreate the correct environment in which your scripts get executed. I’m going to do this for root
but you can do this with every user.
First: edit your crontab
Once it has had the chance to execute once you can remove it.
Second: let’s create a new script called run-as-cron
that will run your scripts with the correct environment cron is running.
Finally, debug your problematic script like this: