healthchecks/templates/docs/monitoring_cron_jobs.html
2020-01-23 16:04:15 +02:00

109 lines
4.4 KiB
HTML
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<h2>Monitoring Cron Jobs</h2>
<p>SITE_NAME is perfectly suited for monitoring cron jobs.
Let's look at an example: a machine with the following cron job:</p>
<div class="highlight"><pre><span></span>$ crontab -l
<span class="c1"># m h dom mon dow command</span>
<span class="m">8</span> <span class="m">6</span> * * * /home/user/backup.sh
</pre></div>
<p>You can use SITE_NAME to get a notification whenever the <code>backup.sh</code> script does not
complete successfully. Here is how to set that up.</p>
<ol>
<li>
<p>If you have not already, sign up for a free SITE_NAME account.</p>
</li>
<li>
<p>In your SITE_NAME account, <strong>add a new check</strong>.</p>
<p>Note: in SITE_NAME, a <strong>check</strong> represents a single service you want to
monitor. For example, a single cron job. For each additional cron job you will
create another check. SITE_NAME pricing plans are structured primarily
around how many checks you can have in the account.</p>
</li>
<li>
<p>Give the check <strong>a meaningful name</strong>. Good naming will become
increasingly important as you add more checks to your account.</p>
</li>
<li>
<p>Edit the check's <strong>schedule</strong>:</p>
<ul>
<li>change its type from "Simple" to "Cron"</li>
<li>enter <code>8 6 * * *</code> in the cron epression field</li>
<li>set the timezone to match your machine's timezone</li>
</ul>
</li>
<li>
<p>Take note of your check's unique <strong>ping URL</strong></p>
</li>
</ol>
<p>Finally, edit your crontab and append a curl or wget call after the command:</p>
<div class="highlight"><pre><span></span>$ crontab -e
<span class="c1"># m h dom mon dow command</span>
<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> PING_URL &gt; /dev/null
</pre></div>
<p>Now, each time your cron job runs, it will send a HTTP request to the ping URL.</p>
<p>Since SITE_NAME knows the schedule of your cron job, it can calculate
the dates and times when the job should run. As soon as your cron job doesn't
report at an expected time, SITE_NAME will send you a notification.</p>
<p>This monitoring technique takes care of various failure scenarios that could
potentially go unnoticed otherwise:</p>
<ul>
<li>The whole machine goes down (power outage, janitor stumbles on wires, VPS provider problems, etc.)</li>
<li>cron daemon is not running, or has invalid configuration</li>
<li>cron does start your task, but the task exits with non-zero exit code</li>
</ul>
<h2>Curl Options</h2>
<p>The extra options tells curl to not print anything to standard output unless
there is an error. Feel free to adjust the curl options to suit your needs.</p>
<table class="table curl-opts">
<tr>
<th>&amp;&amp;</th>
<td>Run curl only if <code>/home/user/backup.sh</code> exits with an exit code 0</td>
</tr>
<tr>
<th>
-f, --fail
</th>
<td>Makes curl treat non-200 responses as errors</td>
</tr>
<tr>
<th>-s, --silent</th>
<td>Silent or quiet mode. Don't show progress meter or error messages.</td>
</tr>
<tr>
<th>-S, --show-error</th>
<td>When used with -s it makes curl show error message if it fails.</td>
</tr>
<tr>
<th>--retry &lt;num&gt;</th>
<td>
If a transient error is returned when curl tries to perform a
transfer, it will retry this number of times before giving up.
Setting the number to 0 makes curl do no retries
(which is the default). Transient error means either: a timeout,
an FTP 4xx response code or an HTTP 5xx response code.
</td>
</tr>
<tr>
<th>&gt; /dev/null</th>
<td>
Redirect curl's stdout to /dev/null (error messages go to stderr,)
</td>
</tr>
</table>
<h2>Looking up Your Machine's Time Zone</h2>
<p>On modern GNU/Linux systems, you can look up the time zone using the
<code>timedatectl status</code> command and looking for "Time zone" in its output:</p>
<div class="highlight"><pre><span></span>$ timedatectl status
Local time: C  2020-01-23 12:35:50 EET
Universal time: C  2020-01-23 10:35:50 UTC
RTC time: C  2020-01-23 10:35:50
<span class="hll"> Time zone: Europe/Riga (EET, +0200)
</span>System clock synchronized: yes
NTP service: active
RTC in local TZ: no
</pre></div>