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

H
Published in: Google Apps Script

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 HTML File

<button onclick="start()">Start</button>
<button onclick="stop()">Stop</button>

<script>
  function start() {
    google.script.run.SuccessHandler(running).startScript();
  }

  function running(e) {
    console.log('Script is running');
  }

  function stop() {
    google.script.run.withSuccessHandler(stopped).stopScript();
  }

  function stopped() {
    console.log('Script has stopped');
  }
</script>

The Server (HTML is served as a web app)

function startScript() {
  do {
    Logger.log('Script running');
    Utilities.sleep(5000);
  } 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').setSandboxMode(
    HtmlService.SandboxMode.IFRAME
  );
}

Also see: How to Stop Google Scripts

📮  Subscribe to our Email Newsletter for Google tips and tutorials!
Published in: Google Apps Script

Looking for something? Find here!

Meet the Author

Web Geek, Google Developer Expert
A
Amit Agarwal

Amit Agarwal is a Google Developer Expert in Google Workspace and Google Apps Script. He holds an engineering degree in Computer Science (I.I.T.) and is the first professional blogger in India. He is the developer of Mail Merge for Gmail and Document Studio. Read more on Lifehacker and YourStory

Get in touch

Google Add-ons

Do more with your Gmail and GSuite account
G

We build bespoke solutions that use the capabilities and the features of Google Workspace for automating business processes and driving work productivity.

  1. Mail Merge with Attachments
    Send personalized email to your Google Contact with a Google Sheet and Gmail
  2. Save Emails and Attachments
    Download email messages and file attachments from Gmail to your Google Drive
  3. Google Forms Email Notifications
    Send email notifications to multiple people when a new Google Form is submitted
  4. Document Studio
    Create beautiful pixel perfect documents merging data from Google Sheets and Google Forms
  5. Creator Studio for Google Slides
    Turn your Google Slides presentations into animated GIFs and videos for uploading to YouTube