DEV Community

Alejandro Riera
Alejandro Riera

Posted on • Originally published at ariera.github.io on

6 3

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

Heroku

Build apps, not infrastructure.

Dealing with servers, hardware, and infrastructure can take up your valuable time. Discover the benefits of Heroku, the PaaS of choice for developers since 2007.

Visit Site

Top comments (0)

Qodo Takeover

Introducing Qodo Gen 1.0: Transform Your Workflow with Agentic AI

Rather than just generating snippets, our agents understand your entire project context, can make decisions, use tools, and carry out tasks autonomously.

Read full post

Best practices for optimal infrastructure performance with Magento

Running a Magento store? Struggling with performance bottlenecks? Join us and get actionable insights and real-world strategies to keep your store fast and reliable.

Tune in to the full event

DEV is partnering to bring live events to the community. Join us or dismiss this billboard if you're not interested. ❤️