Skip to content
A service that survives a suspend

A service that survives a suspend

A suspended machine stops everything in it; when it resumes, it boots again. A program you start by hand, or in the background of a shell, is gone after that. Make it a service and the machine starts it at every boot, restarts it when it crashes, and keeps its output.

1. Create the service

Give it a name, a restart policy and the command after --:

space-env services create worker --restart on-failure --env QUEUE=default --cwd /root -- sh -c "while sleep 30; do date; done"

It starts at once. --restart always (the default) also restarts it after a clean exit; on-failure only after a crash; never leaves it.

If it depends on another service (a database before the app), name that one with --needs, and the machine starts them in order at boot.

2. Check it

space-env services list
space-env services logs worker --lines 20

The log is /var/log/space-env/worker.log; tail -f follows it.

3. Suspend and resume

From your machine:

space machines sleep <machine> --wait
space machines wake <machine> --wait
space ssh <machine> space-env services list

The service runs again, with a new pid. Programs keep nothing in memory across a suspend: keep state on disk, which survives.

4. Change or stop it

space-env services restart worker
space-env services stop worker
space-env services start worker
space-env services delete worker

stop is sticky: the service stays stopped across reboots until start. To change its command or environment, delete it and create it again.

Serving HTTP

A web server as a service plus ingress serves the machine over HTTPS. The machine must be running for a request to reach it: the edge does not resume a suspended machine yet.

Last updated on