I recently created a number of cron jobs to backup databases, backup files offsite, virus scan and perform a number of tasks (at various times) I put a call out on Twitter as to the best program to monitor these jobs.
@nixcraft What’s a good tool for monitoring cron job events and output? #Thanks
— Simon Fearby (Aussie DevSecOps) (@FearbySoftware) June 8, 2019
George Liu from https://community.centminmod.com/ kindly replied with this advice.
one i have used before https://t.co/SbsMfYFUg9
— George Liu (@centminmod) June 9, 2019
I visited https://healthchecks.io/. A quick Google reveals this software is open source too, nice.
Create a healthchecks.io account
I created an account at https://healthchecks.io/ by visiting https://healthchecks.io/accounts/login/ and clicking the signup link then entering my email address.
I click the link that was sent to my email account to log in.
I then logged in and reviewed my account settings at https://healthchecks.io/accounts/profile and set a password (I then click the link in my email to set a password).
After my password was setup I visited https://healthchecks.io/
Pricing
The service is free to use up to 20 checks, then it’s $20/mo for 100 cron job checks. I am using the free version.
View the pricing page here.
Creating Monitors (Checks)
I logged into https://healthchecks.io and clicked the project icon (the one with my email)
I deleted the sample item then clicked ‘Add Check‘
I was prompted to give a name to the job and configure it.
If you click the ‘Name’ (unnamed) item a form will appear.
TIP: Name the jobs something memorable or similar to the job in “crontab”
I completed the form and clicked ‘Save‘
I click the gear icon for the job to configure it
After clicking the Settings icon I could see the full details of the job
- Name
- Description
- How to ping it (HTTP or email)
- Current Status (Pause or Ping Now)
- Timezone (Important, make sure this is the same timezone as your server)
- Expected Schedule
- Notification Method
- Log
I could see the custom URL I needed to request in my cronjob to make the job pass the success test (a random GUI will be created for each job)
https://hc-ping.com/######-####-####-####-############
I added the following to the end of my desired cron job entry (I have removed my GUID for security reasons and replaced it with #’s)
&& curl -fsS --retry 3 https://hc-ping.com/########-####-####-####-######## > /dev/null
The complete cron entry looks like this (not sure what “12 12 * * *” means, check here)
12 12 * * * /bib/bash /scripts/thescript.sh && curl -fsS --retry 3 https://hc-ping.com/########-####-####-####-######## > /dev/null
I can also configure the job to expect when to receive an indication when the job has been run.
You can set a schedule with sliders..
Or you can set a schedule by pasting in the cron job format and see when the job will be expected to run in the future.
Visit https://crontab.guru/ to learn how to set crontab schedules.
After you save the job settings you will be returned to the main page.
At any time you can reopen the job’s to adjust settings.
You can even see details about the requested clients are accessing the URLs, nice.
I created 17 jobs on 2x servers
I setup 2 jobs that will fail just to see what failed jobs look like.
Badges
You can also use badges to quickly display the status of cron jobs in other apps or webpages (by embedding an image)
I created a quick HTML (PHP) file that linked to the badge images. I have swapped out my badge images for security but paste in yours (from above)
HTML
Cron Jobs
- Server 1:
- Server 2:
- Health Checks:
This is what the simple html file looks like in a browser.
Reports
I logged into the portal and it did not take long for me to start seeing errors.
I had an invalid cron job entry (the wrong user was assigned and the job was failing).
I had an error all this time and did not know it.
My dummy job that was setup to fail did fail.
An email alert was sent to my defined email address, nice.
I could also see the email with the failing event in the log (and an initial test call made with a browser)
I was able to pause the reporting of this job in future.
Conclusion
I really like https://healthchecks.io/ and will continue using it.
Sending Emails
I have added a send email command to the end of my cron job called bach script(s) to be alerted of the jobs running.
sendemail -f [email protected] -t [email protected] -u "CRON: xxx Run (server.com)" -m "/scripts/script.sh" -s smtp.gmail.com:587 -o tls=yes -xu [email protected] -xp YourEmailPasswordGoesHere -a /folder/file-to-attach.log
Read my post here How to send email via G Suite from Ubuntu in the cloud
If you have a G Suite account with 2FA enabled you will need to create a App Specific Password for this to work in your G Suite admin panel.
Links
Related links can be found here
Version:
1.2 Added send email Info
1.1 Initial Post