Skip to content

Task guide

Check a website and alert when it is down

An hourly trigger fetches one address. The script remembers the last result in a script property, so a long outage produces one alert instead of one every hour.

1 · Prepare

Put these pieces in place

  • Pick a public web address that anyone can open without signing in. A page behind a login always looks down to the script.
  • Use an account that can send email, because the alert goes to the address of the account that owns the script.
  • Create a standalone Apps Script project at script.google.com, or use a project you already have, and keep both functions below in it.

Change these values

Change these values

Make the code yours before you run it.
Url to check
https://example.com

The page the script asks for every hour. Replace it with your own address.

Status property
SITE_STATUS

The script property that remembers a failure, so the same outage is not reported again and again.

Alert subject
Website check failed

The subject of the email sent when the check fails.

2 · Review before running

What the script can access and change

Permissions you approve

  • Connect to an external service, which lets the script fetch the address in the code.
  • Send email from your Google account. The alert goes to the address of the account that owns the script.
  • Create and view triggers in this Apps Script project, and store one small value with it.

Effects of one run

  • Asks for the address once an hour and writes the status code into the Execution log every time.
  • Sends one alert email on the first failed check, and no more until the address answers again.
  • Stores the word down under SITE_STATUS only after the outage alert email sends. On the first good check, it sends a short back up email, then removes the property.
  • Never changes the website it checks, and never emails anyone except the script owner.

3 · Copy

Paste this into the Apps Script editor

Create or open a script file, replace the example values marked above, then save the project.

Code.gs
const URL_TO_CHECK = 'https://example.com'; // The page the script asks for every hour. Replace it with your own address.
const STATUS_PROPERTY = 'SITE_STATUS'; // The script property that remembers a failure, so the same outage is not reported again and again.
const ALERT_SUBJECT = 'Website check failed'; // The subject of the email sent when the check fails.

function createWebsiteCheckTrigger() {
  const alreadyInstalled = ScriptApp.getProjectTriggers().some((trigger) => {
    return trigger.getHandlerFunction() === 'checkWebsite';
  });
  if (alreadyInstalled) return;

  ScriptApp.newTrigger('checkWebsite')
    .timeBased()
    .everyHours(1)
    .create();
}

function checkWebsite() {
  const properties = PropertiesService.getScriptProperties();
  const lastStatus = properties.getProperty(STATUS_PROPERTY);
  const owner = Session.getEffectiveUser().getEmail();

  let statusCode = 0;
  try {
    statusCode = UrlFetchApp.fetch(URL_TO_CHECK, { muteHttpExceptions: true }).getResponseCode();
  } catch (error) {
    statusCode = 0;
  }

  const isUp = statusCode >= 200 && statusCode < 400;
  Logger.log(URL_TO_CHECK + ' answered with status code ' + statusCode);

  if (!isUp && lastStatus !== 'down') {
    GmailApp.sendEmail(owner, ALERT_SUBJECT, URL_TO_CHECK + ' answered with status code ' + statusCode + '. A zero means no answer arrived at all.');
    properties.setProperty(STATUS_PROPERTY, 'down');
    return;
  }

  if (isUp && lastStatus === 'down') {
    GmailApp.sendEmail(owner, 'Website is back up', URL_TO_CHECK + ' answered with status code ' + statusCode + '.');
    properties.deleteProperty(STATUS_PROPERTY);
  }
}

4 · Run safely

Follow these steps in order

  1. Test against a working address

    Paste both functions, save, then choose checkWebsite and press Run. Approve access when Google asks. Open the Execution log and look for the status code. A status code is the short number a web server sends back to say how the request went, and anything from 200 to 399 counts as up here. A healthy site sends no email.

  2. Test the alert on purpose

    Change the address to one that fails, such as https://example.com/page-that-is-not-there, and run checkWebsite. You get one email. Run it a second time and no further email arrives, because the script has already noted the failure.

  3. Watch it recover

    Put the working address back and run checkWebsite once more. You get the short back up email, and the saved note is cleared. Check Project Settings, Script Properties, to see that SITE_STATUS is gone.

  4. Install the hourly trigger

    Run createWebsiteCheckTrigger once. Running it again is harmless because it stops when a trigger for checkWebsite already exists. Open Triggers in the left sidebar to see the hour timer.

5 · Confirm

How to tell it worked

The Executions list shows a run each hour with a status code in the log, and your inbox holds at most two emails per outage, one when it starts and one when it ends.

If the result is different

Troubleshooting

The email says status code 0.
A zero means the request did not finish, so no code came back. Check the address for a typo, and make sure it starts with https and can be opened in a browser.
An alert arrives every hour for the same outage.
Open Project Settings, Script Properties, and check that SITE_STATUS is being saved. Repeated alerts also happen when a site keeps switching between answering and failing.
The site is clearly down but no email arrives.
The script has already reported it. Delete SITE_STATUS under Project Settings, Script Properties, to let the next failing check alert you again.
A page that works in your browser reports 401 or 403.
Those codes mean the server wants a sign-in, and the script is not signed in to that site. Check a public page instead.

Technical details

Names used in this script

Open these details when you want to adapt the example or look up an exact service or method name.

Show Apps Script symbols and service pages

Keep going