> ## Documentation Index
> Fetch the complete documentation index at: https://docs.nebius.com/llms.txt
> Use this file to discover all available pages before exploring further.

# How to stop and start Compute virtual machines

To proceed with [maintenance](./maintenance), stop and start virtual machines [manually](#how-to-stop-and-start-vms-manually) or [automatically](#how-to-stop-and-start-vms-automatically-by-using-autohealing). If you need to keep your VMs running, [contact the support team](https://console.nebius.com/support/create-ticket) and consult them about the possibility of rescheduling maintenance of the affected VMs.

<Warning>
  Do not stop your VM by using Linux commands, such as `shutdown` or `halt`. Compute considers this as a failure, reboots the VM automatically and continues charging for this VM. Use Nebius AI Cloud interfaces instead and follow the instructions below.
</Warning>

## How to stop and start VMs manually

<Tabs group="interfaces">
  <Tab title="Web console">
    1. In the sidebar, go to <Icon icon="https://mintcdn.com/nebius-ai-cloud/1Ha0sWR6e1mnIaHS/_assets/sidebar/compute.svg?fit=max&auto=format&n=1Ha0sWR6e1mnIaHS&q=85&s=b91340217b08a1456d88ae0347f281d1" width="16" height="16" data-path="_assets/sidebar/compute.svg" /> **Compute** → **Virtual machines**.

    2. Find VMs with the maintenance label.

           <Accordion title="Example">
             <Frame>
               <img src="https://mintcdn.com/nebius-ai-cloud/3vrg63wLg56IgavR/_assets/compute/maintenance-label-for-one-vm.png?fit=max&auto=format&n=3vrg63wLg56IgavR&q=85&s=7e0e5645e6e70a1b07b0b31fb768ffb9" alt="maintenance-label" width="650" height="510" data-path="_assets/compute/maintenance-label-for-one-vm.png" />
             </Frame>
           </Accordion>

    3. Click <Icon icon="https://mintcdn.com/nebius-ai-cloud/1Ha0sWR6e1mnIaHS/_assets/button-vellipsis.svg?fit=max&auto=format&n=1Ha0sWR6e1mnIaHS&q=85&s=e80b8e57c43bfd117679262e6a1334ad" width="12" height="24" data-path="_assets/button-vellipsis.svg" /> → **Stop** for these VMs.

    4. In the window that opens, confirm stopping the VM.

    5. When the VM status changes to **Stopped**, click <Icon icon="https://mintcdn.com/nebius-ai-cloud/1Ha0sWR6e1mnIaHS/_assets/button-vellipsis.svg?fit=max&auto=format&n=1Ha0sWR6e1mnIaHS&q=85&s=e80b8e57c43bfd117679262e6a1334ad" width="12" height="24" data-path="_assets/button-vellipsis.svg" /> → **Start**.

    6. In the window that opens, confirm starting the VM.
  </Tab>

  <Tab title="CLI">
    1. Check whether maintenance is scheduled for a VM:

       ```bash theme={null}
       nebius compute instance get --id <VM_ID>
       ```

       If the output contains the `status.maintenance_event_id` parameter, this means that you need to stop and start this VM.

    2. Stop the VM:

       ```bash theme={null}
       nebius compute instance stop --id <VM_ID>
       ```

    3. Start the VM:

       ```bash theme={null}
       nebius compute instance start --id <VM_ID>
       ```
  </Tab>
</Tabs>

## How to stop and start VMs automatically by using autohealing

You can create a VM with the autohealing function. It allows a VM to automatically stop and start each time maintenance is scheduled. As a result, the applications on this VM are highly available.

<Warning>
  Use autohealing only if the automatic VM shutdown is safe for the workloads on this VM. If you cannot stop the VM at any time, it is better to manage the VM [manually](#how-to-stop-and-start-vms-manually).
</Warning>

To create a VM that stops and starts automatically during maintenance events:

1. [Create a service account](../../iam/service-accounts/manage#create-a-service-account).

2. Add it to the default `viewers` group:

   <Tabs group="interfaces">
     <Tab title="CLI">
       1. Get the ID of the `viewers` group:

          ```bash theme={null}
          nebius iam group get-by-name --name viewers \
            --parent-id <tenant_ID> \
            --format json | jq -r '.metadata.id'
          ```

          In the command, specify your [Tenant ID](/iam/get-tenants#cli).

       2. Get the ID of the service account:

          ```bash theme={null}
          nebius iam service-account get-by-name \
            --name <service_account_name> \
            --format json | jq -r '.metadata.id'
          ```

       3. Add the service account to the `viewers` group:

          ```bash theme={null}
          nebius iam group-membership create \
            --parent-id <viewers_group_ID> \
            --member-id <service_account_ID>
          ```
     </Tab>
   </Tabs>

3. [Create a VM](./manage). In its configuration, specify the following:

   * [Settings that allow connections](./connect#set-up-the-vm) to this VM.
   * Created service account:

     ```bash theme={null}
     nebius compute instance create \
     - <<EOF
     {
       "spec": {
         ...
         "service_account_id": "<service_account_ID>"
       },
       ...
     }
     EOF
     ```

4. [Connect to the VM](./connect#connect-to-the-vm-by-using-ssh).

5. Install `jq` on the VM:

   <CodeGroup>
     ```bash Ubuntu theme={null}
     sudo apt-get install jq
     ```

     ```bash macOS theme={null}
     brew install jq
     ```
   </CodeGroup>

6. Go to the `/usr/local/bin/` directory:

   ```bash theme={null}
   cd /usr/local/bin/
   ```

7. Create the `vm_maintenance.sh` file:

   ```bash theme={null}
   sudo nano vm_maintenance.sh
   ```

8. Add the following Bash script to the file:

   ```bash theme={null}
   #!/bin/bash

   INSTANCE_ID_FILE="/mnt/cloud-metadata/instance-id"
   CHECK_INTERVAL=600 # 10 minutes in seconds

   # Check if the Nebius AI Cloud CLI is installed
   check_nebius_installed() {
       if ! command -v nebius &> /dev/null; then
           echo "$(date) - Nebius utility not found, exiting..."
           exit 1
       fi
   }

   get_instance_id() {
       if [[ -f "$INSTANCE_ID_FILE" ]]; then
           cat "$INSTANCE_ID_FILE"
       else
           echo "File with the virtual machine ID is not found"
           exit 1
       fi
   }

   # Handle a maintenance event
   handle_maintenance_event() {
       # Add custom actions before stopping the virtual machine
       echo "$(date) - Custom actions before stopping the virtual machine"
       sudo shutdown -h +1 "Restarting virtual machine due to maintenance"
   }

   check_nebius_installed

   # main loop
   while true; do
       INSTANCE_ID=$(get_instance_id)
       if [[ -z "$INSTANCE_ID" ]]; then
           echo "Virtual machine ID is empty"
           exit 1
       fi

       RESPONSE=$(nebius compute instance get --id "$INSTANCE_ID" --format json)
       if echo "$RESPONSE" | jq -e '.status.maintenance_event_id' >/dev/null; then
           echo "$(date) - Maintenance event detected"
           handle_maintenance_event
       else
           echo "$(date) - No maintenance event detected"
       fi

       sleep "$CHECK_INTERVAL"
   done
   ```

9. Make the file executable:

   ```bash theme={null}
   sudo chmod +x /usr/local/bin/vm_maintenance.sh
   ```

10. To make the script run continuously and restart it when the VM fails or reboots, add a `systemd` unit to the script:

    ```bash theme={null}
    sudo bash -c 'cat > /etc/systemd/system/vm_maintenance.service << EOF
    [Unit]
    Description=VM Maintenance Event Handler
    After=network.target

    [Service]
    ExecStart=/usr/local/bin/vm_maintenance.sh
    Restart=always
    User=root
    Environment=PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
    WorkingDirectory=/usr/local/bin
    StandardOutput=append:/var/log/vm_maintenance.log
    StandardError=append:/var/log/vm_maintenance.log

    [Install]
    WantedBy=multi-user.target
    EOF'
    ```

11. Start the service created with the `systemd` unit, and let it restart in case of the VM reboot:

    ```bash theme={null}
    sudo systemctl daemon-reload
    sudo systemctl start vm_maintenance
    sudo systemctl enable vm_maintenance
    sudo systemctl status vm_maintenance
    ```

    The output example is the following:

    ```text theme={null}
    Created symlink /etc/systemd/system/multi-user.target.wants/vm_maintenance.service → /etc/systemd/system/vm_maintenance.service.
    ● vm_maintenance.service - VM Maintenance Event Handler
         Loaded: loaded (/etc/systemd/system/vm_maintenance.service; enabled; vendor preset: enabled)
         Active: active (running) since Fri 2025-04-25 10:19:19 UTC; 214ms ago
       Main PID: 28530 (vm_maintenance.)
          Tasks: 6 (limit: 19056)
         Memory: 24.3M
            CPU: 25ms
         CGroup: /system.slice/vm_maintenance.service
                 ├─28530 /bin/bash /usr/local/bin/vm_maintenance.sh
                 └─28535 nebius compute instance get --id computeinstance-*** --format json

    Apr 25 10:19:19 computeinstance-*** systemd[1]: Started VM Maintenance Event Handler.
    ```

Now, the script runs continuously, and the VM stops and starts during maintenance events.
