Sending keep-alive messages from a VM to prevent automatic shutdown or suspend

Auto-shutdown monitors several types of activity to determine whether an environment is idle. To prevent an environment from automatically suspending or shutting down while other types of activity are in progress (for example, during an active SSH session or a database backup), you can send keep-alive messages to the Skytap environment metadata service from within the VM. These reset the auto-shutdown timer and notify Skytap that the environment is still in use.

Contents

Keep-alive message format

To send a keep-alive message from within a VM, make a GET request to the environment metadata service and append the ?reset_autosuspend=1 query parameter.

For example:

curl -G http://gw/skytap?reset_autosuspend=1

To prevent automatic shutdown or suspend while a service is running, create a script that periodically sends a keep-alive message while the service is active. We’ve provided some sample scripts below.

Notes

  • Because the keep-alive request is made to the environment metadata service, the script must be run within the VM.
  • The environment metadata service doesn’t require external Internet access or Skytap credentials to use.

Sample scripts

These scripts should be treated as sample code. They haven’t been tested on all operating systems. Thoroughly test them before using them.

Sample Bash script to prevent automatic shutdown or suspend if mysqld is running

This script checks whether the mysqld service is running. If the service is running, it sends a keep-alive message to the environment metadata service.

#!/bin/sh
SERVICE='mysqld'
if ps ax | grep -v grep | grep $SERVICE > /dev/null
then
  curl -G http://gw/skytap?reset_autosuspend=1
else
  echo "$SERVICE is not running"
fi

Sample Bash script to prevent automatic shutdown or suspend while there are active SSH connections

This script checks for established SSH connections on the VM. While there are active connections, it sends a keep-alive message to the environment metadata service every 4 minutes.

#!/bin/bash
while true
do
CONNECTIONS=`netstat -aa | grep ssh | grep ESTABLISHED`

if [ -n "$CONNECTIONS" ]; then
  curl -G http://gw/skytap?reset_autosuspend=1
  sleep 240
else
  echo "No Active Connections"
fi
done

Sample PowerShell script to prevent automatic shutdown or suspend if Firefox is running

This script checks whether there is an active Firefox session. If there is a session, it sends a keep-alive message to the environment metadata service.

$ProcessActive = Get-Process firefox -ErrorAction SilentlyContinue
if($ProcessActive -ne $null)
{
  curl -G http://gw/skytap?reset_autosuspend=1
}
else
{
  echo "Process not running"
}

Sample PowerShell script to prevent automatic shutdown or suspend if there is an active RDP connection

This script checks for an active RDP connection on the VM. If there is an active connection, it sends a keep-alive message to the environment metadata service. This script can be run periodically using Windows Scheduled Tasks.

Notes

# Run the qwinsta.exe and parse the output
$queryResults = (qwinsta /server:$ServerName | foreach { (($_.trim() -replace "\s+",","))} | ConvertFrom-Csv)

# Pull the session information from each instance
ForEach ($queryResult in $queryResults) {
  $RDPUser = $queryResult.USERNAME
  $sessionType = $queryResult.SESSIONNAME

  # Gathering user information.
  If (($RDPUser -match "[a-z]") -and ($RDPUser -ne $NULL)) {
    # When running interactively, uncomment the Write-Host line below to show the output to screen
    # Write-Host $ServerName logged in by $RDPUser on $sessionType
    $SessionList = $SessionList + $RDPUser + " on " + $sessionType
  }
}
if ($SessionList -like '*rdp-tcp*') {Invoke-WebRequest http://gw/skytap?reset_autosuspend=1}
Else {"No incoming remote RDP sessions."}