>_ IzzyLab

Hands-on task

This task is step 9 of the course “Cron: scheduled jobs”

System cron: checking the site every 2 minutes

medium~15 min

Write an executable script /usr/local/bin/site-up: it takes a URL as its first argument and, if the URL answered HTTP 200, prints UP and exits with code 0; otherwise it prints DOWN and exits with code 1. Then set up a system cron job as the file /etc/cron.d/site-up: as user root, every 2 minutes, check http://localhost:8070/ping, appending the output to /var/log/site-up.log. The cron daemon isn't running in the sandbox — start it yourself.

What you'll practise:
  • cron.d
  • system crontab
  • curl http_code
  • exit code
  • */N

Topic breakdown

Besides users' personal crontabs, cron has system files — /etc/crontab and the /etc/cron.d/ directory, where packages and admins put their jobs as separate files. The format there is one field longer: between the schedule and the command you say which user to run as. cron filters file names in cron.d: files with a dot in the name (say, .sh or .bak) are skipped. The check script itself reports the result in two ways — a line for the log and an exit code for automation; the HTTP status comes from curl -w '%{http_code}'.

What's checked

  1. the site-up script is executable
  2. live URL: UP and code 0
  3. a 404 answer: DOWN and code 1
  4. a service that's down: DOWN and code 1
  5. /etc/cron.d/site-up checks every 2 minutes as root
  6. the cron daemon is running

What it looks like