Skip to content

Commit ec07da6

Browse files
committed
docs(templates): Document startup_script_behavior in-depth
Fixes #7759
1 parent a2e1290 commit ec07da6

File tree

1 file changed

+58
-1
lines changed

1 file changed

+58
-1
lines changed

docs/templates/index.md

+58-1
Original file line numberDiff line numberDiff line change
@@ -145,12 +145,14 @@ by all child processes of the agent, including SSH sessions. See the
145145
[Coder Terraform Provider documentation](https://registry.terraform.io/providers/coder/coder/latest/docs/resources/agent)
146146
for the full list of supported arguments for the `coder_agent`.
147147

148-
#### startup_script
148+
#### `startup_script`
149149

150150
Use the Coder agent's `startup_script` to run additional commands like
151151
installing IDEs, [cloning dotfiles](../dotfiles.md#templates), and cloning
152152
project repos.
153153

154+
**Note:** By default, the startup script is executed in the background allowing users to access the workspace before the script completes. If you want to change this, see `startup_script_behavior` below.
155+
154156
```hcl
155157
resource "coder_agent" "coder" {
156158
os = "linux"
@@ -163,10 +165,12 @@ resource "coder_agent" "coder" {
163165
# that does not require root permissions. Note that /tmp may be mounted in tmpfs which
164166
# can lead to increased RAM usage. To avoid this, you can pre-install code-server inside
165167
# the Docker image or VM image.
168+
echo "Installing code-server..."
166169
curl -fsSL https://code-server.dev/install.sh | sh -s -- --method=standalone --prefix=/tmp/code-server --version 4.8.3
167170
168171
# The & prevents the startup_script from blocking so the next commands can run.
169172
# The stdout and stderr of code-server is redirected to /tmp/code-server.log.
173+
echo "Starting code-server..."
170174
/tmp/code-server/bin/code-server --auth none --port 13337 >/tmp/code-server.log 2>&1 &
171175
172176
# var.repo and var.dotfiles_uri is specified
@@ -175,15 +179,56 @@ curl -fsSL https://code-server.dev/install.sh | sh -s -- --method=standalone --p
175179
176180
# clone repo
177181
ssh-keyscan -t rsa github.com >> ~/.ssh/known_hosts
182+
echo "Cloning ${var.repo}..."
178183
git clone --progress git@github.com:${var.repo}
179184
180185
# use coder CLI to clone and install dotfiles
186+
echo "Cloning dotfiles..."
181187
coder dotfiles -y ${var.dotfiles_uri}
182188
183189
EOT
184190
}
185191
```
186192

193+
We recommend that startup scripts always have an end, meaning that long running
194+
processes should be run in the background. This is usually achieved by adding
195+
`&` to the end of the command. For example, `sleep 10 &` will run the command in
196+
the background and allow the startup script to complete.
197+
198+
If a backgrounded command (`&`) writes to stdout or stderr, the startup script
199+
will not complete until the command completes or closes the file descriptors.
200+
To avoid this, you can redirect the stdout and stderr to a file. For example,
201+
`sleep 10 >/dev/null 2>&1 &` will redirect the stdout and stderr to `/dev/null`
202+
(discard) and run the command in the background.
203+
204+
PS. Notice how each step starts with `echo "..."` to provide feedback to the user
205+
about what is happening? This is especially useful when the startup script
206+
behavior is set to blocking because the user will be informed about why they're
207+
waiting to access their workspace.
208+
209+
#### `startup_script_behavior`
210+
211+
Use the Coder agent's `startup_script_behavior` to change the behavior between
212+
`blocking` and `non-blocking` (default). The blocking behavior is recommended
213+
for most use cases because it allows the startup script to complete before the
214+
user accesses the workspace. For example, let's say you want to check out a very
215+
large repo in the startup script. If the startup script is non-blocking, the
216+
user may log in via SSH or open the IDE before the repo is fully checked out.
217+
This can lead to a poor user experience.
218+
219+
Whichever behavior is enabled, the user can still choose to override it by
220+
specifying the appropriate flags (or environment variables) in the CLI when
221+
connecting to the workspace. For example, `coder ssh --no-wait` will connect to
222+
the workspace without waiting for the startup script to complete.
223+
224+
```hcl
225+
resource "coder_agent" "coder" {
226+
os = "linux"
227+
arch = "amd64"
228+
startup_script_behavior = "blocking"
229+
startup_script = "echo 'Starting...'"
230+
```
231+
187232
### Start/stop
188233

189234
[Learn about resource persistence in Coder](./resource-persistence.md)
@@ -372,6 +417,18 @@ practices:
372417
- The Coder agent shutdown script logs are typically stored in `/tmp/coder-shutdown-script.log`
373418
- This can also happen if the websockets are not being forwarded correctly when running Coder behind a reverse proxy. [Read our reverse-proxy docs](https://coder.com/docs/v2/latest/admin/configure#tls--reverse-proxy)
374419

420+
### Startup script in progress, incomplete workspace
421+
422+
WIP
423+
424+
### Startup script in progress, session before completion
425+
426+
WIP
427+
428+
### Startup script error
429+
430+
WIP
431+
375432
### Agent does not become ready
376433

377434
If the agent does not become ready, it means the [startup script](https://registry.terraform.io/providers/coder/coder/latest/docs/resources/agent#startup_script) is still running or has exited with a non-zero status. This also means the [login before ready](https://registry.terraform.io/providers/coder/coder/latest/docs/resources/agent#login_before_ready) option hasn't been set to true.

0 commit comments

Comments
 (0)