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.

112 lines
4.4 KiB

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