Recipe

Restart a bot from code

A restart command for your admin bot, a nightly restart, or a restart after you change its settings: send one request, then wait for it to come back.

← API docsUpdated

Browse the docs

What you need

An API key with the control.restart permission, limited to the server it restarts, and the server's 8-character identifier from the panel. Put the key in the SILLYDEV_API_KEY environment variable.

Python

This sends the restart, then reads the server's state every few seconds until it is running again. The restart itself happens on the node; the request returns as soon as it is accepted.

restart.py

import os
import time

import requests

API = "https://sillydev.co.uk/panel/api/client"
SERVER = "1a7ce997"
HEADERS = {
    "Authorization": f"Bearer {os.environ['SILLYDEV_API_KEY']}",
    "Accept": "application/json",
}


def state() -> str:
    response = requests.get(f"{API}/servers/{SERVER}/resources", headers=HEADERS, timeout=10)
    response.raise_for_status()
    return response.json()["attributes"]["current_state"]


response = requests.post(
    f"{API}/servers/{SERVER}/power", headers=HEADERS, json={"signal": "restart"}, timeout=10
)
response.raise_for_status()

# Give it a moment to start stopping, then wait for it to be back.
time.sleep(3)
for _ in range(60):
    if state() == "running":
        print("Back online")
        break
    time.sleep(5)
else:
    raise SystemExit("Still not running after five minutes; check the console.")

JavaScript

The same in Node.js 18 or later, Bun or Deno, with the built-in fetch.

restart.mjs

const API = 'https://sillydev.co.uk/panel/api/client';
const SERVER = '1a7ce997';
const headers = {
    Authorization: `Bearer ${process.env.SILLYDEV_API_KEY}`,
    Accept: 'application/json',
    'Content-Type': 'application/json',
};
const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));

const restart = await fetch(`${API}/servers/${SERVER}/power`, {
    method: 'POST',
    headers,
    body: JSON.stringify({ signal: 'restart' }),
});
if (!restart.ok) throw new Error(`Restart refused: HTTP ${restart.status}`);

await sleep(3000);
for (let attempt = 0; attempt < 60; attempt++) {
    const response = await fetch(`${API}/servers/${SERVER}/resources`, { headers });
    const { attributes } = await response.json();
    if (attributes.current_state === 'running') {
        console.log('Back online');
        process.exit(0);
    }
    await sleep(5000);
}
throw new Error('Still not running after five minutes; check the console.');

Restarting from inside the bot

A bot can restart itself the same way, for example from an owner-only command. Do not wait for it to come back in that case: the restart stops the very process that asked. Send the request and let it go.

If the server was stopped, restart starts it. To only start a stopped server, send start; to stop one, stop. kill stops it at once without a clean shutdown, so keep it for a bot that will not stop.

FAQ

Questions

Why does the restart take a while sometimes?

When many servers on a node start at once, starts wait their turn in a queue, and paid servers go first. The console says so while it waits. The bot is not broken; it starts as soon as its turn comes.

Read next