Monitoring cron jobs is as difficult as it is important. Because these tasks usually run on servers that are not accessible from outside HTTP calls, and the only way to get information from them is cron jobs sending logs itself to another service.
The Cron Job Monitoring API does exactly that. Every time your services are running, they report that they are OK or there is a problem, via a ping event to this API. If your service is delayed in sending pings until grace time, Cron Job Monitoring API will notify you immediately by e-mail. It's that simple!
Let's start using it step by step
1. Create a new "check"
We start using the API by creating a new check. Don't forget to give it a beautiful and memorable name. POST /check endpoint will create a new check and give you a unique "ping url" back. The next step should be for your application to start pinging this ping url.
curl --location \
--request POST 'https://api.apilayer.com/cron_monitoring/check' \
--header 'apikey: API KEY' \
--data-raw 'My First Check'
When a new check is created all of it information is returned with the response
{
"id": "5dfaf9d0-3a2c-40dd-902e-cc0fb352b86b",
"name": "My First Check",
"status": "unknown",
"ping_url": "https://api.apilayer.com/cron_monitoring/p/5dfaf9d0-3a2c-40dd-902e-cc0fb352b86b",
"schedule": {
"type": "cron",
"cron_expression": "0 * * * *",
"grace_time": 60,
"timezone": "UTC",
"last_check_at": null,
"cron_description": "Every hour"
},
"integrations": [
{
"type": "email",
"email": "[email protected]",
"owner": "[email protected]"
}
]
}
2. Pinging the API regularly
Place the following piece of code in your cron jobs that pinging for this check wherever you see fit. You can put as often as you want. It can also happen inside a for loop, only 1 time. It's entirely up to you. The Cron Job Monitoring API will store the last 1000 ping requests for you, and you can get the whole ping list with GET / check /
curl --location \
--request POST 'https://api.apilayer.com/cron_monitoring/ping/5dfaf9d0-3a2c-40dd-902e-cc0fb352b86b' \
--header 'apikey: API KEY' \
--data-raw 'This is some description'
3. Pinging for special statuses
If you want to notify the API that the application has a problem somewhere, simply add / fail to the end of your ping_url and send it. You can also send / start and / finish status codes. Messages sent with / fail status will intelligently shorten the grace time and allow you to receive earlier alerts if the cron is delayed.
curl --location \
--request POST 'https://api.apilayer.com/cron_monitoring/ping/5dfaf9d0-3a2c-40dd-902e-cc0fb352b86b/fail' \
--header 'apikey: API KEY' \
--data-raw 'This is the ping description.'
4. Getting the ping information
You can get the description of your check and how often it will check the service with GET / check /
curl --location \
--request GET 'https://api.apilayer.com/cron_monitoring/check/5dfaf9d0-3a2c-40dd-902e-cc0fb352b86b' \
--header 'apikey: API KEY'
5. Retrieving the logs for a check
If you want to view all "pings" at once, you can use the GET / pings endpoint.
curl --location \
--request GET 'https://api.apilayer.com/cron_monitoring/check/5dfaf9d0-3a2c-40dd-902e-cc0fb352b86b/pings' \
--header 'apikey: API KEY'
6. Settting the crontab expression
By default, your API will set the crontab expression to 0 * * * * once a day. If you want to change this, you can use the PUT / schedule / cron endpoint.
curl --location \
--request PUT 'https://api.apilayer.com/cron_monitoring/check/5dfaf9d0-3a2c-40dd-902e-cc0fb352b86b/schedule/cron' \
--header 'apikey: API KEY' \
--data-raw '0 */3 * * *'
7. Updating the grace time
Your API takes grace time 60 minutes by default. If you want to change this value PUT / schedule / grace.
curl --location \
--request PUT 'https://api.apilayer.com/cron_monitoring/check/5dfaf9d0-3a2c-40dd-902e-cc0fb352b86b/schedule/grace' \
--header 'apikey: API KEY' \
--data-raw '90'
8. Changing the timezone/
The API will take UTC as the default timezone. If you want to change it, you can use the PUT / schedule / timezone endpoint.
curl --location \
--request PUT 'https://api.apilayer.com/cron_monitoring/check/5dfaf9d0-3a2c-40dd-902e-cc0fb352b86b/schedule/timezone' \
--header 'apikey: API KEY' \
--data-raw 'UTC+04:00'
That is all! This information covers the basics of your API. You can find more details in the API documentation.