What is cronjob monitoring in PHP?
If you’re using a 3rd party API for cronjobs, or using Cronmon or something similar, monitoring cronjobs in PHP helps you ensure scheduled tasks run as expected.
For example, you might have a nightly backup or report generation.
Tracking their success and failures lets you respond quickly to issues.
Track cronjobs with Operational
Operational is an open-source platform for tracking events in your applications.
You can use it to log cronjob runs, failures, and performance metrics.
Setting up Operational
- Go to app.operational.co and sign up for a free account.
- Create a new project for your PHP application.
- Navigate to the project settings to find your API key.
- Copy the API key and store it in a safe place or environment variable.
Code examples
<?php
$apiKey = 'API_KEY';
$payload = [
'name' => 'cronjob_success',
'avatar' => '✅',
'content' => 'Daily report cronjob finished successfully in 85 seconds'
];
$ch = curl_init('https://api.operational.co/v1/events');
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Authorization: Bearer ' . $apiKey,
'Content-Type: application/json'
]);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($payload));
$result = curl_exec($ch);
curl_close($ch);
echo $result;
?>
This example logs a successful cronjob run.
<?php
$apiKey = 'API_KEY';
$payload = [
'name' => 'cronjob_failure',
'avatar' => '🚨',
'content' => 'Backup cronjob failed with exit code 1'
];
// ... send HTTP request as above ...
?>
This example reports a cronjob failure.
<?php
$apiKey = 'API_KEY';
$payload = [
'name' => 'cronjob_skipped',
'avatar' => '⏭️',
'content' => 'Cleanup cronjob skipped due to active lock file'
];
// ... send HTTP request as above ...
?>
This example tracks a skipped cronjob due to a lock.
Conclusion
Using Operational to monitor your PHP cronjobs can save you time and reduce headaches.
You get real-time visibility into job status, run times, and errors.
Learn more on the Operational website or try the playground to copy and paste snippets.