How to Suspend Execution of a Google App Script While it is Running

A commonly requested feature of Mail Merge was that users wanted the ability to stop the merge process (and thus the underlying Google Script) after hitting the start button.

The maximum execution time limit of any Google Apps Script is about 5 minutes and the script will terminate itself automatically after the time is up. If you are running a script manually from the Google Script Editor, you can click “Cancel” to abort a running script but this option is not available when the script is running through an HTML Service powered web app or as a Google Add-on.

Here’s a little snippet that will show you how to abruptly stop a running script that is executing from outside the Script Editor. The idea is that you set up a property when the Stop button is pressed. The running script watches this property value and if it is set to “STOP”, the script pauses.


The Server (HTML is served as a web app)

function startScript() {
  do {
    Logger.log("Script running");
  } while (keepRunning());
  return "OK";

function keepRunning() {
  var status = PropertiesService.getScriptProperties().getProperty("run") || "OK";
  return status === "OK" ? true : false;

function stopScript() {
  PropertiesService.getScriptProperties().setProperty("run", "STOP");
  return "Kill Signal Issued";

function doGet(e) {
  PropertiesService.getScriptProperties().setProperty("run", "OK");
  return HtmlService.createHtmlOutputFromFile('html')

Also see: How to Stop Google Scripts