A service is a long-running program on your VM, such as a web server or a database. It comes back after every wake, unless you stopped it, and it starts again when it ends.
The examples use a VM named api and a service named web. Set VM to the VM’s id and SI_API_KEY to a key from API keys:
export VM=6f1c0f0e8aexport SI_API_KEY=si_...Add a service
Section titled “Add a service”si svm svc add writes a definition to /etc/slashinfra/services/web.json on the VM’s disk and starts the program as user. The definition stays on the disk across a sleep, so the service comes back after a wake.
Inside the VM, the same subcommands are on svc; si svm svc sends them from your laptop. The VM comes before the command, as in si svm svc api add web .... Without it, si uses the VM set with si use.
si svm svc api add web --port 3000 --dir /home/user --env PYTHONUNBUFFERED=1 -- python3 -m http.server 3000webcurl -sS -X POST "https://$VM.slashinfra.dev/usr/bin/svc?arg=add&arg=web&arg=--port&arg=3000&arg=--dir&arg=/home/user&arg=--env&arg=PYTHONUNBUFFERED%3D1&arg=--&arg=python3&arg=-m&arg=http.server&arg=3000" \ -H "Authorization: Bearer $SI_API_KEY"webimport os, requests
res = requests.post( f"https://{os.environ['VM']}.slashinfra.dev/usr/bin/svc", params=[ ("arg", "add"), ("arg", "web"), ("arg", "--port"), ("arg", "3000"), ("arg", "--dir"), ("arg", "/home/user"), ("arg", "--env"), ("arg", "PYTHONUNBUFFERED=1"), ("arg", "--"), ("arg", "python3"), ("arg", "-m"), ("arg", "http.server"), ("arg", "3000"), ], headers={"Authorization": f"Bearer {os.environ['SI_API_KEY']}"},)res.raise_for_status()print(res.text, end="")webconst url = new URL(`https://${process.env.VM!}.slashinfra.dev/usr/bin/svc`)for (const arg of [ 'add', 'web', '--port', '3000', '--dir', '/home/user', '--env', 'PYTHONUNBUFFERED=1', '--', 'python3', '-m', 'http.server', '3000',]) { url.searchParams.append('arg', arg)}const res = await fetch(url, { method: 'POST', headers: { Authorization: `Bearer ${process.env.SI_API_KEY!}` },})if (!res.ok) throw new Error(await res.text())console.log((await res.text()).trim())webThe words after -- are the program and its arguments, run without a shell. For a pipeline, a variable or a redirection, use /bin/bash -c '...'. The program must stay in the foreground. When its leader ends, the VM ends the rest of its processes and starts it again. A program that should run once belongs in a job.
A new service starts within a second.
Open the port of a service
Section titled “Open the port of a service”--port names the TCP port the program listens on. A preview at https://3000-<id>.slashinfra.dev waits up to 10 seconds while the service is starting. A request made during a start can reach the program. The program must listen on 0.0.0.0 or localhost.
The port also decides what a sleep keeps. A running service that declares a port is saved in memory and its process resumes after the wake. A service without a port stops for a sleep on disk and starts again after the wake. A service you stopped stays stopped.
To share the address through a link or make it public, open the port. See Web addresses.
List services
Section titled “List services”The list has one row per definition, by name. STATE is the state of the service and PID is the process id of the program. RESTARTS counts the restarts after a crash, and PORT is the declared port or -. A service with no process shows - in PID.
-
Open SVMs and select
api. -
Select Terminal. A terminal to the VM opens in a new tab.
-
Run the command below.
svcmanages the services of the VM.Terminal window svc lsOutput NAME STATE PID RESTARTS PORTweb running 1234 0 3000
si svm svc api lsNAME STATE PID RESTARTS PORTweb running 1234 0 3000curl -sS -X POST "https://$VM.slashinfra.dev/usr/bin/svc?arg=ls" \ -H "Authorization: Bearer $SI_API_KEY"NAME STATE PID RESTARTS PORTweb running 1234 0 3000import os, requests
res = requests.post( f"https://{os.environ['VM']}.slashinfra.dev/usr/bin/svc", params={"arg": "ls"}, headers={"Authorization": f"Bearer {os.environ['SI_API_KEY']}"},)res.raise_for_status()print(res.text, end="")NAME STATE PID RESTARTS PORTweb running 1234 0 3000const res = await fetch(`https://${process.env.VM!}.slashinfra.dev/usr/bin/svc?arg=ls`, { method: 'POST', headers: { Authorization: `Bearer ${process.env.SI_API_KEY!}` },})if (!res.ok) throw new Error(await res.text())console.log((await res.text()).trim())NAME STATE PID RESTARTS PORTweb running 1234 0 3000Read the log of a service
Section titled “Read the log of a service”svc logs prints the whole log of a service, standard output and standard error together. The log is /var/log/services/web.log on the VM’s disk. It survives a sleep and the deletion of the service. --follow keeps printing new lines until you stop it.
si svm svc api logs webServing HTTP on 0.0.0.0 port 3000 (http://0.0.0.0:3000/) ...curl -sS -X POST "https://$VM.slashinfra.dev/usr/bin/svc?arg=logs&arg=web" \ -H "Authorization: Bearer $SI_API_KEY"Serving HTTP on 0.0.0.0 port 3000 (http://0.0.0.0:3000/) ...import os, requests
res = requests.post( f"https://{os.environ['VM']}.slashinfra.dev/usr/bin/svc", params=[("arg", "logs"), ("arg", "web")], headers={"Authorization": f"Bearer {os.environ['SI_API_KEY']}"},)res.raise_for_status()print(res.text, end="")Serving HTTP on 0.0.0.0 port 3000 (http://0.0.0.0:3000/) ...const res = await fetch(`https://${process.env.VM!}.slashinfra.dev/usr/bin/svc?arg=logs&arg=web`, { method: 'POST', headers: { Authorization: `Bearer ${process.env.SI_API_KEY!}` },})if (!res.ok) throw new Error(await res.text())console.log((await res.text()).trim())Serving HTTP on 0.0.0.0 port 3000 (http://0.0.0.0:3000/) ...Stop a service
Section titled “Stop a service”Stop ends the program and every process it started, then records the stop on the definition. The service stays stopped across a sleep and a wake. The command waits up to 15 seconds for stopped; a program that does not end answers with its state.
si svm svc api stop webweb stoppedcurl -sS -X POST "https://$VM.slashinfra.dev/usr/bin/svc?arg=stop&arg=web" \ -H "Authorization: Bearer $SI_API_KEY"web stoppedimport os, requests
res = requests.post( f"https://{os.environ['VM']}.slashinfra.dev/usr/bin/svc", params=[("arg", "stop"), ("arg", "web")], headers={"Authorization": f"Bearer {os.environ['SI_API_KEY']}"},)res.raise_for_status()print(res.text, end="")web stoppedconst res = await fetch(`https://${process.env.VM!}.slashinfra.dev/usr/bin/svc?arg=stop&arg=web`, { method: 'POST', headers: { Authorization: `Bearer ${process.env.SI_API_KEY!}` },})if (!res.ok) throw new Error(await res.text())console.log((await res.text()).trim())web stoppedStart a service
Section titled “Start a service”Start clears the stop, starts the program now and starts it again after every wake. A service that already runs keeps its process. The command waits up to 15 seconds for running. A service with a port is running when the port accepts a connection.
si svm svc api start webweb runningcurl -sS -X POST "https://$VM.slashinfra.dev/usr/bin/svc?arg=start&arg=web" \ -H "Authorization: Bearer $SI_API_KEY"web runningimport os, requests
res = requests.post( f"https://{os.environ['VM']}.slashinfra.dev/usr/bin/svc", params=[("arg", "start"), ("arg", "web")], headers={"Authorization": f"Bearer {os.environ['SI_API_KEY']}"},)res.raise_for_status()print(res.text, end="")web runningconst res = await fetch(`https://${process.env.VM!}.slashinfra.dev/usr/bin/svc?arg=start&arg=web`, { method: 'POST', headers: { Authorization: `Bearer ${process.env.SI_API_KEY!}` },})if (!res.ok) throw new Error(await res.text())console.log((await res.text()).trim())web runningRestart a service
Section titled “Restart a service”Restart stops the program and starts it again, and resets RESTARTS to 0. Use it after you change the program or its files; the definition keeps its flags. The command waits up to 15 seconds for running.
si svm svc api restart webweb runningcurl -sS -X POST "https://$VM.slashinfra.dev/usr/bin/svc?arg=restart&arg=web" \ -H "Authorization: Bearer $SI_API_KEY"web runningimport os, requests
res = requests.post( f"https://{os.environ['VM']}.slashinfra.dev/usr/bin/svc", params=[("arg", "restart"), ("arg", "web")], headers={"Authorization": f"Bearer {os.environ['SI_API_KEY']}"},)res.raise_for_status()print(res.text, end="")web runningconst res = await fetch(`https://${process.env.VM!}.slashinfra.dev/usr/bin/svc?arg=restart&arg=web`, { method: 'POST', headers: { Authorization: `Bearer ${process.env.SI_API_KEY!}` },})if (!res.ok) throw new Error(await res.text())console.log((await res.text()).trim())web runningDelete a service
Section titled “Delete a service”Delete removes the definition and stops the program. The log stays at /var/log/services/web.log, and the command waits up to 10 seconds for stopped. A service that another definition needs cannot be deleted until you delete that definition.
si svm svc api rm webweb removedcurl -sS -X POST "https://$VM.slashinfra.dev/usr/bin/svc?arg=rm&arg=web" \ -H "Authorization: Bearer $SI_API_KEY"web removedimport os, requests
res = requests.post( f"https://{os.environ['VM']}.slashinfra.dev/usr/bin/svc", params=[("arg", "rm"), ("arg", "web")], headers={"Authorization": f"Bearer {os.environ['SI_API_KEY']}"},)res.raise_for_status()print(res.text, end="")web removedconst res = await fetch(`https://${process.env.VM!}.slashinfra.dev/usr/bin/svc?arg=rm&arg=web`, { method: 'POST', headers: { Authorization: `Bearer ${process.env.SI_API_KEY!}` },})if (!res.ok) throw new Error(await res.text())console.log((await res.text()).trim())web removedOptions
Section titled “Options”| Field | Type | Default | Allowed values | What it changes |
|---|---|---|---|---|
name (argument) |
string | Required | 1 to 64 letters, digits, - and _, starting with a letter or digit; daemon and units are reserved |
The service name and its definition file. It is unique on the VM. |
port (--port) |
integer | None | 1 to 65535 | The TCP port the program listens on. A preview waits while the service is starting. |
needs (--needs) |
string | None | Names of services of the same VM, as A,B |
The services that must be running before this one starts. |
dir (--dir) |
string | /home/user |
An absolute path | The working directory of the program. |
env (--env) |
string | None | KEY=VALUE, repeated; the key has letters, digits and _, and does not start with SVM_ |
A variable of the service’s process. |
program (after --) |
array | Required | A program and its arguments; the first word is searched in PATH when it has no / |
The program to run, without a shell. |
ref (<SVM>) |
string | The VM set with si use |
A VM name, or its id | The VM whose services these are. |
project (--project) |
string | default |
A project name, or its proj_ id |
The project of the VM. SI_PROJECT applies when the flag is absent. |
Scroll horizontally to see all columns.
--needs accepts only names of services on the same VM. An unknown name, or a cycle, keeps svc from applying any change until you add the missing service or delete the definition that names it.
States
Section titled “States”When a program ends without a stop, the VM starts it again after 1 second. The wait then grows to 2, 4, 8, 16 and at most 30 seconds. RESTARTS counts the restarts, and the state is backoff while the VM waits.
| State | What it means for you | What you can do |
|---|---|---|
running |
The program runs. With a declared port, the port accepts a connection. | Read the log, stop, restart or delete the service. |
starting |
The program runs but its declared port does not accept a connection yet. | Wait; read the log if it stays. A preview waits up to 10 seconds. |
backoff |
The program ended and the VM waits to start it again. RESTARTS counts the restarts. |
Read the log to find why it ends; fix the program, or stop the service. |
stopped |
No process runs. The definition is stopped, or a service it needs is not running. |
Start it, or delete it. |
Scroll horizontally to see all columns.
Errors
Section titled “Errors”| Code | When | Fix |
|---|---|---|
svm_not_found |
The VM name or id names no VM of your organization or key scope. | Check the name, the id and the project. |
svm_unreachable |
The node of the VM did not answer. | Wait for Retry-After and retry. |
not_ready |
A preview of the declared port waits on a wake and the VM’s readiness port does not answer in its timeout. | Check the log with si svm svc api logs web, then retry. |
Scroll horizontally to see all columns.
The VM’s svc reports a refused command in the reply. The HTTP status is 200, X-Exit-Code is 1, and si exits 1. The message names the fix, for example svc: no service web; svc ls shows the services, svc: service web already exists; use svc restart web, or svc: web is needed by db; remove it first.
Errors that any request can return are in the error catalog.
Permissions
Section titled “Permissions”Every operation on this page goes to the VM’s own host. A key reaches it when it belongs to the VM’s organization and includes the VM’s project. The VM host checks that scope and does not check a key action. A grant reaches only the methods, paths and arguments it names; see Grants.
| Operation | Who can | Key action |
|---|---|---|
| Add a service | Any member with a key that includes the VM’s project | None |
| List services | Any member with a key that includes the VM’s project | None |
| Read the log of a service | Any member with a key that includes the VM’s project | None |
| Stop a service | Any member with a key that includes the VM’s project | None |
| Start a service | Any member with a key that includes the VM’s project | None |
| Restart a service | Any member with a key that includes the VM’s project | None |
| Delete a service | Any member with a key that includes the VM’s project | None |
Scroll horizontally to see all columns.
Limits
Section titled “Limits”- A VM has no count limit on services:
svcstarts every definition in/etc/slashinfra/servicesthat is not stopped, each in its own cgroup and with its own log. - A service name has 1 to 64 letters, digits,
-and_, and starts with a letter or digit.daemonandunitsare reserved. --portis 1 to 65535.- A crash restarts the service after 1 second, then 2, 4, 8, 16 and at most 30 seconds.
- A stop sends the service’s first signal (SIGTERM by default; the guest
svc add --stop-signal INTselects SIGINT). It gives the program and its processes 10 seconds, then stops what is left. - A preview of a declared port waits up to 10 seconds while the service is
starting. - A running service that declares a port is saved in memory at the memory deadline and resumes after the wake. A service without a port stops for a sleep on disk and starts again after the wake, unless you stopped it.
- A log rotates when the service starts and the file is at least 10 MiB. It becomes
<name>.log.1, replacing the previous rotation. - A service does not keep the VM awake. A timer or cron job inside the VM does not fire on a sleeping VM. Use a schedule for work at a set time.
The full list is in Limits.
- Web addresses: open the port your service listens on.
- Schedules: run work at a set time, which a timer inside the VM cannot do.
- Sleep and wake: what a VM keeps across a sleep.