In this article, We are going to Learn How to test a Cron Job?
Like a Pro 😎😎
These are exact strategies I have used to test multiple Cron jobs over 5 years of time
The software utility Cron is a time-based job scheduler in Unix-like computer operating systems
People who set up and maintain software environments use cron to schedule jobs (commands or shell scripts) to run periodically at fixed times, dates, or intervals.
Below are the few points/Pre-made Test cases for the Cron Job Testing:-
Open the Corntab – Its an online tool that will help you to Check the Cron time.
You can enter the cron time and it will tell you when this cron will trigger.
Note down the time and verify if its correct one.
To verify quickly Mock the Cron time by 1-2 minutes by changing it for quick testing (if it’s long for an hour/day long). – High
Ask the devs to create a shortcut for it. In the server or any other way to directly run it by you so that you can test it when you need it.(saves time)
This way, You can easily run the cron at any time by own and debug it fast and quick
Ask dev to add the logs for monitor purposes and monitor it while testing.
Use below command for logs
- * * * * /path/script.sh &> /path/script.log or tail -f /var/log/cron
Do testing by doing changing things as we do in CRUD(Not always, Crud means update/delete etc whichever is relevant) and verify results. – High
Break something the job relies on upon and schedule it to run again in a few minutes – Verify the results.
After deployment – Validate it with real data also. – High
Keep system time and user system time in mind while testing CRON’s as scheduling is by server GMT time usually.
Negative scenario: What if CRON is stopped the midway execution, any impacts it will have?
Negative – Discuss with dev if there is any possibility of the same Cron getting run multiple times.
Negative – Discuss with dev if it fails to the job – What is the risk/ solution for it(did we get the mail if it fails ?)
Test for scenarios which shouldn’t get affected by Cron job(e.g. the other Cron should not affect other cron)
Let’s take an Example
This is sample Cron job template in crontab
0/1 * * * * /usr/bin/python /home/my_username/hello.py > /home/my_username/log.txt
Check and Verify its scenarios.
If you enjoyed reading this article, kindly give it a share. Your share is extremely helpful to spread our message and help more bloggers like you.
Be sure to subscribe for more videos like this!