How to debug CRON tasks?
Some people call it the Devil behind the scenes of web development. It's the CRON, and it's an extremely powerful tool that can do wonderful things to automate tasks on your server. This can save you hours of work every week.
Debugging CRON jobs should be as basic and essential to your diagnostics toolbox as log analysis. However, very few people know how to do it. That's where this guide comes in! There are numerous reasons why a CRON job can fail, each with a different response and solution. This guide will help you understand what needs to happen on both your server and app side to start debugging these errors.
How to find out where the error is located when cron jobs aren't working as expected?
The logging attribute of a cron job is very important in troubleshooting. If it is set to "silent", then the command contains the notation below at the end, which means that the result of the cron job is not written anywhere.
ls -al > /dev/null 2>&1
In order to fix the error, change the cron job to "broadcast" by removing the /dev/null 2>&1 part at the end of the command. This causes the results of execution to be logged to one of the folders.
/home/username/mail/new
/home/username/mail/cur
The error can therefore be detected by looking at the contents of the log file.
It is essential to set the cron job to "broadcast" only for a short period, i.e. only when the error is being corrected. Once the error has been resolved, we need to "silence" them again by re-adding the removed part, otherwise, the log files will be created endlessly on the hosting package taking up your allocated disk space.
Conclusion
In this post, we've discovered how to start the debugging process of Cron jobs. When in trouble let's hope we don't have to debug a cron job, but if the time comes and you are unable, then hopefully the method below makes the process easy enough.