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 does not
  16. run on time or does not 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 cron job definition and append a curl or wget call
  27. after the command:
  28. ```bash
  29. $ crontab -e
  30. # m h dom mon dow command
  31. 8 6 * * * /home/user/backup.sh && curl -fsS --retry 3 PING_URL > /dev/null
  32. ```
  33. Now, each time your cron job runs, it will send a HTTP request to the ping URL.
  34. Since SITE_NAME knows the schedule of your cron job, it can calculate
  35. the dates and times when the job should run. As soon as your cron job doesn't
  36. report at an expected time, SITE_NAME will send you a notification.
  37. This monitoring technique takes care of various failure scenarios that could
  38. potentially go unnoticed otherwise:
  39. * The whole machine goes down (power outage, janitor stumbles on wires, VPS provider problems, etc.)
  40. * cron daemon is not running, or has invalid configuration
  41. * cron does start your task, but the task exits with non-zero exit code
  42. ## Curl Options
  43. The extra options tells curl to not print anything to standard output unless
  44. there is an error. Feel free to adjust the curl options to suit your needs.
  45. <table class="table curl-opts">
  46. <tr>
  47. <th>&amp;&amp;</th>
  48. <td>Run curl only if <code>/home/user/backup.sh</code> exits with an exit code 0</td>
  49. </tr>
  50. <tr>
  51. <th>
  52. -f, --fail
  53. </th>
  54. <td>Makes curl treat non-200 responses as errors</td>
  55. </tr>
  56. <tr>
  57. <th>-s, --silent</th>
  58. <td>Silent or quiet mode. Don't show progress meter or error messages.</td>
  59. </tr>
  60. <tr>
  61. <th>-S, --show-error</th>
  62. <td>When used with -s it makes curl show error message if it fails.</td>
  63. </tr>
  64. <tr>
  65. <th>--retry &lt;num&gt;</th>
  66. <td>
  67. If a transient error is returned when curl tries to perform a
  68. transfer, it will retry this number of times before giving up.
  69. Setting the number to 0 makes curl do no retries
  70. (which is the default). Transient error means either: a timeout,
  71. an FTP 4xx response code or an HTTP 5xx response code.
  72. </td>
  73. </tr>
  74. <tr>
  75. <th>&gt; /dev/null</th>
  76. <td>
  77. Redirect curl's stdout to /dev/null (error messages go to stderr,)
  78. </td>
  79. </tr>
  80. </table>
  81. ## Looking up Your Machine's Time Zone
  82. On modern GNU/Linux systems, you can look up the time zone using the
  83. `timedatectl status` command and looking for "Time zone" in its output:
  84. ```text hl_lines="6"
  85. $ timedatectl status
  86. Local time: C  2020-01-23 12:35:50 EET
  87. Universal time: C  2020-01-23 10:35:50 UTC
  88. RTC time: C  2020-01-23 10:35:50
  89. Time zone: Europe/Riga (EET, +0200)
  90. System clock synchronized: yes
  91. NTP service: active
  92. RTC in local TZ: no
  93. ```