TIL: How to debug cron jobs

ariera

Alejandro Riera

Posted on February 27, 2018

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 and add one line

crontab -e
* * * * * /usr/bin/env > /root/cron-env
Enter fullscreen mode Exit fullscreen mode

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.

cd /root
touch run-as-cron
chmod +x run-as-cron
vim run-as-cron
#!/bin/bash
/usr/bin/env -i $(cat /root/cron-env) "$@"
Enter fullscreen mode Exit fullscreen mode

Finally, debug your problematic script like this:

/root/run-as-cron /the/problematic/script --with arguments --and parameters
Enter fullscreen mode Exit fullscreen mode
💖 💪 🙅 🚩
ariera
Alejandro Riera

Posted on February 27, 2018

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

Opposite Colours Tool
codepen Opposite Colours Tool

November 5, 2024

Starting to Rust
rust Starting to Rust

November 4, 2024