# Background work

Start a job in your VM, disconnect, then read its log and exit code over the API.

Source: https://slashinfra.com/docs/build/background-work
Last modified: 2026-11-30T16:36:19.000Z

You start a job in your VM and disconnect. The job keeps running, and you read its log and exit code later.

## Before you start

- A VM. [Create one from your terminal](https://slashinfra.com/docs/start/cli) and keep its id. 
- An organization key. 
- A command to run in the VM. This guide runs tests in `/home/user/app`. Use your project’s directory and test command.

Set these once in your terminal. Step 1 prints the job number, so set `JOB` to it.

```bash
export SI_API_KEY=<your-organization-key>
VM=<your-vm-id>
JOB=3
```

## 1. Start a job

Send the script as the body of a `POST` to `/usr/bin/job`. With `arg=-q` the reply is the job number alone. The reply also carries a `Location` header that names the job’s log.

### curl

```bash
curl -sS -H "Authorization: Bearer $SI_API_KEY" --data-binary 'cd /home/user/app && npm test' "https://$VM.slashinfra.dev/usr/bin/job?arg=-q"
```

Output

```text
3
```

### Python

```python
import os
import requests

res = requests.post(
f"https://{os.environ['VM']}.slashinfra.dev/usr/bin/job",
headers={"Authorization": f"Bearer {os.environ['SI_API_KEY']}"},
params={"arg": "-q"},
data=b"cd /home/user/app && npm test",
)
print(res.text)
```

### TypeScript

```ts
const res = await fetch(`https://${process.env.VM!}.slashinfra.dev/usr/bin/job?arg=-q`, {
method: 'POST',
headers: { Authorization: `Bearer ${process.env.SI_API_KEY!}` },
body: 'cd /home/user/app && npm test',
})
console.log(await res.text())
```

## 2. Disconnect

Close your terminal or let your script end. The job runs after the request ends, and its files and log stay in the VM. A busy job keeps the VM awake, so it runs until it ends.

## 3. Read the log

The log is at `/var/log/jobs/<number>.log`. Read it at any time, while the job runs or after it ends.

### curl

```bash
curl -sS -H "Authorization: Bearer $SI_API_KEY" "https://$VM.slashinfra.dev/var/log/jobs/$JOB.log"
```

Output

```text
...
Tests: 42 passed, 0 failed
```

### Python

```python
import os
import requests

res = requests.get(
f"https://{os.environ['VM']}.slashinfra.dev/var/log/jobs/{os.environ['JOB']}.log",
headers={"Authorization": f"Bearer {os.environ['SI_API_KEY']}"},
)
print(res.text)
```

### TypeScript

```ts
const res = await fetch(`https://${process.env.VM!}.slashinfra.dev/var/log/jobs/${process.env.JOB!}.log`, {
headers: { Authorization: `Bearer ${process.env.SI_API_KEY!}` },
})
console.log(await res.text())
```

## 4. Read the exit code

A `POST` to `/usr/bin/job` with `arg=wait` and the job number waits for the job to end. The `X-Exit-Code` header carries the job’s exit code.

### curl

```bash
curl -sS -D - -o /dev/null -X POST -H "Authorization: Bearer $SI_API_KEY" "https://$VM.slashinfra.dev/usr/bin/job?arg=wait&arg=$JOB"
```

Output

```text
HTTP/2 200
X-Exit-Code: 0
```

### Python

```python
import os
import requests

res = requests.post(
f"https://{os.environ['VM']}.slashinfra.dev/usr/bin/job",
headers={"Authorization": f"Bearer {os.environ['SI_API_KEY']}"},
params={"arg": ["wait", os.environ["JOB"]]},
)
print(res.headers["X-Exit-Code"])
```

### TypeScript

```ts
const url = new URL(`https://${process.env.VM!}.slashinfra.dev/usr/bin/job`)
url.searchParams.append('arg', 'wait')
url.searchParams.append('arg', process.env.JOB!)

const res = await fetch(url, {
method: 'POST',
headers: { Authorization: `Bearer ${process.env.SI_API_KEY!}` },
})
console.log(res.headers.get('X-Exit-Code'))
```

A wait can end before the job does. When the reply has `X-Timeout: 1`, the job still runs. Send the same request again.

## Check it works

You should now see the job’s log and an `X-Exit-Code` that matches the result in the log. `0` means the job passed.

## If it doesn’t work

| Symptom | Cause | Fix |
| --- | --- | --- |   
| `X-Exit-Code: 137` | Something killed the job before it ended. | Read the log to find the last line the job printed. | 
| `X-Timeout: 1` | The wait ended before the job did. | Send the wait again. |

## Clean up

> Caution
> 
> 
> 
> Deletes the VM and its files. The job’s log in `/var/log/jobs/` goes with it. Copy any file you need first. Deletion cannot be undone.

### curl

```bash
curl -sS -o /dev/null -w "%{http_code}\n" -X DELETE -H "Authorization: Bearer $SI_API_KEY" "https://api.slashinfra.com/svms/$VM"
```

Output

```text
204
```

### Python

```python
import os
import requests

res = requests.delete(
f"https://api.slashinfra.com/svms/{os.environ['VM']}",
headers={"Authorization": f"Bearer {os.environ['SI_API_KEY']}"},
)
print(res.status_code)
```

### TypeScript

```ts
const res = await fetch(`https://api.slashinfra.com/svms/${process.env.VM!}`, {
method: 'DELETE',
headers: { Authorization: `Bearer ${process.env.SI_API_KEY!}` },
})
console.log(res.status)
```

## Next

- [Sleep and wake](https://slashinfra.com/docs/how/sleep-and-wake): a busy job keeps the VM awake, and a sleep on disk ends its process. 
- [Services](https://slashinfra.com/docs/platform/services): a program the VM starts again after it wakes, for work that must keep running. 
- [Limits](https://slashinfra.com/docs/reference/limits): the command and output limits that a job’s program runs under. 
- [Commands and jobs](https://slashinfra.com/docs/platform/commands-and-jobs): every command, terminal and job operation, with its options and limits.
