# Scripts, files and services

> For the complete documentation index, see [llms.txt](https://docs.ringleader.dev/llms.txt).


> Scripts run setup commands on a workstation, files put content at a path, and services keep a program running.


Scripts run setup commands on a workstation, files put content at a path, and services keep
a program running. You declare them once, and Ringleader keeps the workstation matching
them. Use them for what packages, devtools and tool configuration don't cover.

## Setup scripts

A setup script runs on the workstation during a configuration pass, after the packages,
devtools, files and code are in place. Scripts run in the order you declare them. A
script's `phase` says who runs it. `system`, the default, runs it as root, and `user` runs
it as the login user, in their home folder.

A script's `runPolicy` says when it runs:

- `once`, the default, runs each version of the script once, so editing the script runs it
  again.
- `onChange` also runs it again when a path in `watchPaths` changes, such as a code source
  it builds. With no `watchPaths`, it works like `once`.
- `always` runs it on every pass.
- `onRecycle` never runs it in an ordinary pass. It runs once each time the workstation's
  `spec.recycleNonce` changes, which is how a
  [workstation pool](https://docs.ringleader.dev/workstations/lifecycle/pools-leases/#member-recycling) cleans a member
  before its next lease.

<div class="callout callout-warning">
  <p class="callout-title">A failed recycle deletes the member</p>
  <div class="callout-body">
    A failing <code>onRecycle</code> script fails the recycle, whatever its <code>fatal</code> says, and the pool then
deletes the member.
  </div>
</div>


A script's `env` can carry `${secret:NAME}` references. A script with `failOnError: true`
stops the rest of the pass when it fails, as
[Scripts that stop the pass](https://docs.ringleader.dev/workstations/configuration/step-failures-retries/#scripts-that-stop-the-pass)
explains. A script can also take its body from a
[ConfigMap](https://docs.ringleader.dev/reference/resources/configmap/) instead of carrying it inline.

## Declared files

A file entry puts content at a path on the workstation. The content is inline `content`,
`contentBase64`, a `url`, or a ConfigMap key in `contentFrom`. Inline `content` and
`contentFrom` can carry `${secret:NAME}` references. A file under `~/` belongs to the login
user, and any other file to root, unless you set `owner`. Ringleader creates the folders
above the file when they are missing.

Ringleader writes a file when its entry, or a secret it uses, changes. It doesn't check the
file on the workstation between changes, so an edit made there stays until the entry
changes. By default, a file also stays when you stop declaring it: when you delete its
entry, or the layer that carried it stops applying.

## Removal of undeclared files

With `removeWhenUndeclared: true`, Ringleader deletes the file on the next pass after its
entry leaves the workstation's configuration. You set it on each entry. A configuration
can lose a layer without anyone editing anything, and deleting every undeclared file would
remove healthy files then. Two things to know about it:

- If you take `removeWhenUndeclared` off an entry, Ringleader stops tracking the file on the
  next pass, and the file stays.
- If a `contentFrom` reference can't be resolved, the configuration is incomplete, and
  Ringleader removes no file at all on that pass.

<div class="callout callout-warning">
  <p class="callout-title">Edited files are deleted too</p>
  <div class="callout-body">
    Ringleader doesn&rsquo;t check the file&rsquo;s content before it deletes it, so a file edited on the
workstation is deleted too.
  </div>
</div>


## Managed services

A service keeps a program running on the workstation. You give either a full systemd `unit`
or a `command`, which Ringleader turns into one. By default a service starts at once and
starts again each time the workstation boots. Services start after the scripts.

## Run a setup script

To build a code source each time it changes, give a script `runPolicy: onChange` and watch
the source's path:

```yaml
spec:
  scripts:
    - name: build-app
      phase: user
      runPolicy: onChange
      watchPaths: ["/home/dev/app"]
      content: |
        cd ~/app && make build
```

To keep a long script in one place, take its body from a ConfigMap key with `contentFrom`
instead of `content`, as [Scripts](https://docs.ringleader.dev/reference/resources/workstationconfig/#scripts)
shows.

## Write a file

To put a file on the workstation, list it under `spec.files`:

```yaml
spec:
  files:
    - path: ~/.config/app/settings.toml
      content: |
        theme = "dark"
      removeWhenUndeclared: true
```

## Run a service

To keep a program running, list it under `spec.services`:

```yaml
spec:
  services:
    - name: docs-preview
      command: /home/dev/app/bin/preview --port 4000
      runAs: dev
```

## Next steps

- [Scripts](https://docs.ringleader.dev/reference/resources/workstationconfig/#scripts),
  [Managed services](https://docs.ringleader.dev/reference/resources/workstationconfig/#managed-services) and
  [Files](https://docs.ringleader.dev/reference/resources/workstationconfig/#files) show the fields in an
  example.
- [Users, environment and system settings](https://docs.ringleader.dev/workstations/packages-tools-settings/users-environment-system-settings/)
  covers the login user and the workstation's own settings.

