You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

115 lines
3.8 KiB

  1. # Monitoring Cron Jobs
  2. SITE_NAME is perfectly suited for monitoring cron jobs. All you have to do is
  3. update your cron job command to send a HTTP request to SITE_NAME
  4. after a job completes.
  5. Let's look at an example:
  6. ```bash
  7. $ crontab -l
  8. # m h dom mon dow command
  9. 8 6 * * * /home/user/backup.sh
  10. ```
  11. The above job runs `/home/user/backup.sh` every day at 6:08. The backup
  12. script is presumably a headless, background process. Even if it works
  13. correctly currently, it can start silently failing in future, without
  14. anyone noticing.
  15. You can set up SITE_NAME to notify you whenever the backup script doesn't
  16. run on time and complete successfully. Here are the steps to do that.
  17. 1. If you have not already, sign up for a free SITE_NAME account.
  18. 1. In your SITE_NAME account, **add a new check**.
  19. 1. Give the check **a meaningful name**. Good naming will become
  20. increasingly important as you add more checks to your account.
  21. 1. Edit the check's **schedule**:
  22. * change its type from "Simple" to "Cron"
  23. * enter `8 6 * * *` in the cron epression field
  24. * set the timezone to match your machine's timezone
  25. 1. Take note of your check's unique **ping URL**
  26. Finally, edit your crontab and append a curl or wget call after the command:
  27. ```bash
  28. $ crontab -e
  29. # m h dom mon dow command
  30. 8 6 * * * /home/user/backup.sh && curl -fsS --retry 3 PING_URL > /dev/null
  31. ```
  32. Now, each time your cron job runs, it will send a HTTP request to the ping URL.
  33. Since SITE_NAME knows the schedule of your cron job, it can calculate
  34. the dates and times when the job should run. As soon as your cron job doesn't
  35. report at an expected time, SITE_NAME will send you a notification.
  36. This monitoring technique takes care of various failure scenarios that could
  37. potentially go unnoticed otherwise:
  38. * The whole machine goes down (power outage, janitor stumbles on wires, VPS provider problems, etc.)
  39. * cron daemon is not running, or has invalid configuration
  40. * cron does start your task, but the task exits with non-zero exit code
  41. ## Curl Options
  42. The extra options tells curl to not print anything to standard output unless
  43. there is an error. Feel free to adjust the curl options to suit your needs.
  44. <table class="table curl-opts">
  45. <tr>
  46. <th>&amp;&amp;</th>
  47. <td>Run curl only if <code>/home/user/backup.sh</code> exits with an exit code 0</td>
  48. </tr>
  49. <tr>
  50. <th>
  51. -f, --fail
  52. </th>
  53. <td>Makes curl treat non-200 responses as errors</td>
  54. </tr>
  55. <tr>
  56. <th>-s, --silent</th>
  57. <td>Silent or quiet mode. Don't show progress meter or error messages.</td>
  58. </tr>
  59. <tr>
  60. <th>-S, --show-error</th>
  61. <td>When used with -s it makes curl show error message if it fails.</td>
  62. </tr>
  63. <tr>
  64. <th>--retry &lt;num&gt;</th>
  65. <td>
  66. If a transient error is returned when curl tries to perform a
  67. transfer, it will retry this number of times before giving up.
  68. Setting the number to 0 makes curl do no retries
  69. (which is the default). Transient error means either: a timeout,
  70. an FTP 4xx response code or an HTTP 5xx response code.
  71. </td>
  72. </tr>
  73. <tr>
  74. <th>&gt; /dev/null</th>
  75. <td>
  76. Redirect curl's stdout to /dev/null (error messages go to stderr,)
  77. </td>
  78. </tr>
  79. </table>
  80. ## Looking up Your Machine's Time Zone
  81. On modern GNU/Linux systems, you can look up the time zone using the
  82. `timedatectl status` command and looking for "Time zone" in its output:
  83. ```text hl_lines="6"
  84. $ timedatectl status
  85. Local time: C  2020-01-23 12:35:50 EET
  86. Universal time: C  2020-01-23 10:35:50 UTC
  87. RTC time: C  2020-01-23 10:35:50
  88. Time zone: Europe/Riga (EET, +0200)
  89. System clock synchronized: yes
  90. NTP service: active
  91. RTC in local TZ: no
  92. ```