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.

122 lines
3.9 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 expression 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 5 -o /dev/null PING_URL
  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 in the above example tells curl to retry failed HTTP requests, and
  44. to silence output unless there is an error. Feel free to adjust the curl options to
  45. suit your needs.
  46. **&&**
  47. : Run curl only if `/home/user/backup.sh` exits with an exit code 0.
  48. **-f, --fail**
  49. : Makes curl treat non-200 responses as errors.
  50. **-s, --silent**
  51. : Silent or quiet mode. Hides the progress meter, but also hides error messages.
  52. **-S, --show-error**
  53. : Re-enables error messages when -s is used.
  54. **--retry <num>**
  55. : If a transient error is returned when curl tries to perform a
  56. transfer, it will retry this number of times before giving up.
  57. Setting the number to 0 makes curl do no retries (which is the default).
  58. Transient error is a timeout or an HTTP 5xx response code.
  59. **-o /dev/null**
  60. : Redirect curl's stdout to /dev/null (error messages still go to stderr).
  61. ## Looking up Your Machine's Time Zone
  62. If your cron job consistently pings SITE_NAME an hour early or an hour late,
  63. the likely cause is a timezone mismatch: your machine may be using a different timezone
  64. than what is configured on SITE_NAME.
  65. On modern GNU/Linux systems, you can look up the time zone using the
  66. `timedatectl status` command and looking for "Time zone" in its output:
  67. ```text hl_lines="6"
  68. $ timedatectl status
  69. Local time: C  2020-01-23 12:35:50 EET
  70. Universal time: C  2020-01-23 10:35:50 UTC
  71. RTC time: C  2020-01-23 10:35:50
  72. Time zone: Europe/Riga (EET, +0200)
  73. System clock synchronized: yes
  74. NTP service: active
  75. RTC in local TZ: no
  76. ```
  77. ## Viewing Cron Logs Using `journalctl`
  78. On a systemd-based system, you can use the `journalctl` utility to see system logs,
  79. including logs from the cron daemon.
  80. To see live logs:
  81. ```bash
  82. journalctl -f
  83. ```
  84. To see logs from e.g. the last hour, and only from the cron daemon:
  85. ```bash
  86. journalctl --since "1 hour ago" -t CRON
  87. ```