For the complete documentation index, see llms.txt. Markdown versions of documentation pages are available by appending .md to page URLs.
Skip to content

AutoStopping VMs over SSH

Use the AutoStopping SSH preflight script to reach a stopped VM with an ssh command.

AutoStopping shuts down a VM when it is not in use and starts it again when traffic arrives. For HTTP workloads this is transparent, but a plain ssh command gives up before a stopped VM can finish starting. The SSH preflight script closes that gap: it starts the VM, waits for it to accept connections, and then allows your ssh command to run without any change.

Consider TCP mode instead


What you will learn from this topic

  • Why SSH connections fail against stopped AutoStopping VMs, and how the preflight script fixes it.

  • How the connection flow works once the script is set up.

  • How to install, configure, and add the script to the way your users already connect.

  • How to verify it, troubleshoot common issues, and what the current limitations are.


Why SSH needs a preflight step

AutoStopping reduces your cloud bill by stopping idle VMs and starting them again when they are needed. For web traffic this is seamless: the AutoStopping proxy receives the first request, starts the VM, and holds the request until the VM is ready. The user observes only a slower first page load.

SSH behaves differently. The ssh command expects a response within a few seconds and fails if it does not receive one. A stopped VM needs 30 to 60 seconds to boot and start its SSH service (sshd), which is longer than ssh waits, so the connection fails with Connection timed out during banner exchange before the VM is ready.

Increasing the ssh timeout is not a reliable workaround. The ConnectTimeout setting is often restricted by security policy, and a higher value slows down every failed connection, not only the first start.

The preflight script removes the conflict by doing the waiting before ssh runs. By the time ssh connects, the VM is already up, so no timeout occurs and no SSH configuration changes are required.


How it works

When a user connects to a stopped VM, the script starts the AutoStopping rules that cover the target address and waits until the VM is running and SSH is answering. The script then hands over to ssh, which connects to a VM that is already up. While the session is open, the script reports activity every two minutes, so AutoStopping does not treat the VM as idle and stop it mid-session. When the last session ends, the script stops reporting. The rule's idle timer then runs out as it would for any idle VM, and AutoStopping stops it.

What the script can and cannot do

The script starts VMs and keeps them running while users are connected. When the last session ends, the script stops reporting activity, so the rule's idle timer stops the VM the same way it stops any idle VM. The script does not stop VMs itself, change an AutoStopping rule, or block anyone's connection. Starting a VM does not grant access to it either: as always, only the SSH accounts and keys on the VM decide who can log in.

If anything goes wrong, for example the Harness API is unreachable, the script prints a short message and runs ssh. In the worst case the VM is not started and ssh fails exactly as it would without the script.


Before you begin

  • A VM-based AutoStopping rule that covers the address your users connect to. An idle time of 10 minutes or more is recommended. The minimum of 5 minutes works but leaves less margin if an activity report is lost.

  • A shared Linux host you control (bastion, jump box, build agent, or workstation) where your users run ssh and where you have administrator access to install the script. The script needs bash and curl, so confirm both are installed on the host and install them if they are not.

  • Network access from that host to app.harness.io, or your Harness cluster URL, on port 443.

  • A Harness API key and account ID. The key's role needs view access to the covering rules; see Granular RBAC for AutoStopping. Use a key with a long or no expiration. Go to Add and manage API keys to create one.

The script identifies the rule by the address your users connect to, so that address must stay constant across restarts:

  • Use a private IP, or a static or Elastic public IP. These stay constant, so the script keeps matching the rule.

  • Do not use an automatically assigned public IP. It changes on each restart, so the script stops matching the rule.


Set up SSH preflight access

Set up the script once on the shared host, then connect it to the way your users already run ssh.

Install the script

  1. Download the current version and verify its checksum:

  2. Install the script so users can run but cannot modify it. The script is plain bash, so review it first if you want:

  3. Confirm the version:

Configure the script

With the script installed, the next step is to configure how it reaches your Harness account. The script reads these details, your account ID, the API endpoint, and an API key, from a configuration file.

  1. Create the configuration file:

    This creates /etc/harness/autostop-preflight.conf. Replace <your Harness account ID> with your account ID, and leave timeout and report_interval at their defaults.

  2. Provide your API key. The script reads it from one of two safe places. Choose the one that fits your host:

Use a key file when a single user runs the script. Store the key in its own file, kept separate from the configuration so it is not exposed:

Replace <your API key> with your Harness API key. If you do not have one yet, go to Add and manage API keys to create a key with the permissions listed in Before you begin. The commands lock the file to its owner, so only that user can run the script.

Key file ownership

Use the HARNESS_API_KEY environment variable on a shared host, where several people log in under their own accounts. Each user supplies their own key in their own session, so no single owner-only file is needed:

Replace <your API key> with a Harness API key whose role can view the covering rules. If you do not have one yet, go to Add and manage API keys to create a key with the permissions listed in Before you begin.

Set the variable in each user's shell profile, such as .bashrc, so it persists across sessions, or have a secret manager agent inject it at login. Either way, each person's key stays in their own environment, out of everyone else's reach.

With either method, do not supply the key as a command-line argument, such as --api-key <key>. Process arguments are visible to other users and processes on the host through the process list (ps), and they can be captured in shell history and system logs. The key file and the environment variable avoid this exposure, because the script reads the key without it ever appearing on the command line.

Navigate to Command-line options for the full list of options, their defaults, and the configuration keys and environment variables that set them.

Add it to how people connect

With the script installed and configured, the remaining step is to make it run automatically before ssh connects. The script does not run on its own; it has to be triggered when a user connects. You add it to your users' existing ssh workflow, so their connection runs it automatically and the VM is up by the time ssh opens the session. Select the tab that matches how your users connect: a shared wrapper script, a direct ssh command, or an automated job.

Add the following line above the existing ssh command in your wrapper:

The || true is a safeguard: the script already succeeds on every failure path, but it makes the intent plain to whoever reads the wrapper next. If the wrapper ends in exec ssh …, activity reporting follows the wrapper's process, and exec keeps the same process, so reporting tracks the SSH session and stops when it ends.

Use OpenSSH's own Match exec to run the script before any connection to the managed hosts. Each user adds this to ~/.ssh/config, or you add it to /etc/ssh/ssh_config:

ssh runs the exec command before connecting, so the VM is started and ready by the time the connection is attempted. Users keep typing ssh 10.0.1.23 exactly as they do today.

Let the script run the command itself, so it tracks that command's lifetime precisely:

This is the most reliable form, because the script knows exactly when the session ends. It is the right choice for cron jobs, CI steps, and backup scripts.

Verify it

Confirm the setup works by connecting to a stopped VM and watching it start.

  1. Stop the VM through its AutoStopping rule and wait for the rule to show as Stopped.

  2. Connect the way your users do, adding --verbose to watch what happens:

    You see output similar to this:

The connection then succeeds immediately. In the Harness UI the rule shows as Active, and it stays active for as long as you remain connected.


Command-line options

Every option can also be set in the configuration file or the environment, so the connection command itself needs nothing but --host.

Option
Configuration key
Environment variable
Default

--host <address>

required

--port <port>

22

--account-id <id>

account_id

HARNESS_ACCOUNT_ID

--api-url <url>

api_url

HARNESS_AUTOSTOP_API_URL

https://app.harness.io/gateway/lw/api

--api-key-file <path>

api_key_file

HARNESS_API_KEY_FILE

--config <path>

HARNESS_AUTOSTOP_CONFIG

/etc/harness/autostop-preflight.conf

--timeout <secs>

timeout

300

--report-interval <secs>

report_interval

120

--log-file <path>

log_file

HARNESS_AUTOSTOP_LOG

discard

--no-report

off

--verbose

off

--version

The API key itself is read only from HARNESS_API_KEY or from the key file. It is never accepted as an argument.


Limitations

  • Addresses translated in transit do not match. The address must be one that Harness can see on the VM. Addresses translated by NAT or PrivateLink never match, and the script falls through to a plain ssh.

  • Dynamic public IPs are unreliable. An automatically assigned public IP changes on every restart, so a remembered public address does not reach the VM regardless of this script. Use static or private addresses.

  • A killed reporter process can end a session early. If the reporting process is killed without the session ending, the VM may be stopped mid-session. A generous idle time on the rule is the mitigation.

  • Coverage is by TCP port, not by protocol. The script starts the VM and waits for the SSH port, so anything carried over SSH, such as SFTP, SCP, rsync, git over SSH, and port forwarding, is covered. The script cannot distinguish one SSH-based protocol from another, and it does not detect activity on ports other than the one it checks.


Troubleshooting

The script reports it cannot read the API key file

This has one of two causes: the file is not readable by the user running the script, or it is readable by more than its owner and was refused. Check ownership with ls -l /etc/harness/autostop-api-key: the owner must be the connecting user and the mode must be 600.

The script reports missing required commands

curl is usually the one absent, most often on Ubuntu cloud images. The script names what is missing and allows ssh to run anyway, so install the named command and try again.

The VM is not started and the script gives no useful message

Run the script with --verbose. If it reports no matching rule, the address your users connect to is not the address Harness holds for the VM. Compare it with the VM's address on the rule's detail page. NAT, PrivateLink, and dynamically assigned public IPs all cause this issue.

The VM starts but the connection still fails

The script waited for the SSH port until the timeout elapsed, which usually means a security group or firewall is blocking the port from the host. The connection would have failed anyway; the script only makes it fail later. Check that --timeout is not set too low.

The VM is stopped while someone is still connected

Activity reporting ended early. This occurs when the script is called from a short-lived shell that exits before ssh starts, because in that mode it watches the process that called it. Use the Match exec or -- <command> form instead, or make the wrapper end in exec ssh.

A specific rule fails to start

When Harness cannot start a rule for a session, the reason is recorded in that rule's logs in the Harness UI, not on the host. The host only ever sees that it should hand over to ssh.


Next steps

You have set up SSH preflight access on your shared host, and your users can reach AutoStopping-managed VMs over SSH with little or no change to how they connect and without losing the savings from stopping idle VMs.

Last updated

Was this helpful?