Task guide
Check a website and alert when it is down
Get one email when a website stops answering and one when it is back. An hourly trigger fetches the address, and a saved script property stops the script from emailing you every hour during one outage.
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
- Url to check
https://example.comThe page the script asks for every hour. Replace it with your own address.
- Status property
SITE_STATUSThe script property that remembers a failure, so the same outage is not reported again and again.
- Alert subject
Website check failedThe 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. The code fetches only the address in URL_TO_CHECK.
- Read, compose, send, and permanently delete all your email from Gmail. Google asks for full Gmail access because the code uses GmailApp, but the code only sends alerts to your own address.
- See your primary Google Account email address. The code reads it to know where to send the alert.
- Allow this application to run when you are not present. This lets the hourly trigger check the address while you are away.
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.
- After the outage email is sent, saves the word down as the SITE_STATUS script property. On the first good check after that, it sends a short back up email and then deletes the property.
- 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.
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
Follow these steps in order
Test against a working address
Paste both functions over the starter code and save. Choose checkWebsite in the function dropdown above the code, then click Run. Approve access when Google asks. The Execution log shows the status code. A status code is the short number a web server sends back to say how the request went. Here, anything from 200 to 399 counts as up. A healthy site sends no email.
Test the alert on purpose
Change URL_TO_CHECK to an address that fails, such as https://example.com/page-that-is-not-there, save, and run checkWebsite again. One email with the subject Website check failed arrives. Run it a second time and no further email arrives, because the script has already saved the failure.
Watch it recover
Put the working address back, save, and run checkWebsite once more. An email with the subject Website is back up arrives, and the saved property is deleted. Open Project Settings, the gear icon, and scroll to Script Properties to see that SITE_STATUS is gone.
Install the hourly trigger
Choose createWebsiteCheckTrigger and click Run once. Running it again does not add a second trigger, because it stops when a trigger for checkWebsite already exists. Click Triggers, the clock icon 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.
- Check URL_TO_CHECK for a typo, and make sure it starts with https and opens in a browser. A zero means the request did not finish, so no code came back.
- An alert arrives every hour for the same outage.
- Open Project Settings, scroll to Script Properties, and check that SITE_STATUS is saved with the value down. Repeated alerts also happen when a site keeps switching between answering and failing, because each recovery clears the property.
- The site is clearly down but no email arrives.
- Delete SITE_STATUS under Project Settings, Script Properties, to let the next failing check alert you again. The script sends no second alert while the property holds the value down.
- A page that opens in your browser reports status code 401 or 403.
- Check a public page instead. Those codes mean the server wants a sign-in, and the script is not signed in to that site.
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
Apps Script symbols
Service pages
Keep going
Related tasks, basics, and apps
- Task guide Store settings with PropertiesService
Keep a setting such as an email address out of your code and change it without editing the script. The project stores it as a script property that every run can read.
- Task guide Import data from a web API into Sheets
Pull a seven-day weather forecast from the free Open-Meteo API into a practice spreadsheet. The script checks the response and writes only cells A1:C12 of the Forecast tab, with the source and license links below the data.
- Task guide Run an Apps Script function every day
Make a function run on its own once a day. Run the setup function once, and from then on Apps Script calls the reminder function each day around the hour you chose.
- Object reference HTTP response
UrlFetchApp.fetch() returns an HTTPResponse after calling a URL. Pass muteHttpExceptions: true so error status codes return an HTTPResponse you can check with getResponseCode() and getContentText().
- Object reference Properties
Properties store text values under names you choose, called keys, and keep them between runs. Use getProperty() and setProperty() to read and save them. PropertiesService.getScriptProperties() is shared across the project, user properties are per-person, and document properties are per-file.
- Object reference Trigger builder
ScriptApp.newTrigger(functionName) returns a TriggerBuilder that configures a timeBased(), forSpreadsheet(), or forForm() trigger before create() saves it.
- Google app Script utilities
UrlFetchApp, PropertiesService, and LockService work in any Apps Script project. Use them to call web APIs, save settings between runs, and stop two runs from overlapping.
- Google app Gmail
GmailApp scripts can send messages, search threads, apply labels, and read attachments after you approve Gmail access for the project.
- Google app Triggers
A trigger is a saved schedule or event rule in ScriptApp that runs your function on time, on edit, or on form submit.