# Example scripts

{% hint style="warning" %}
These example scripts may use software that is not be available on every operating system.
{% endhint %}

## Track setup scripts

### Wait for bootstrap

Wait for the Instruqt bootstrap process to complete.&#x20;

{% tabs %}
{% tab title="Bash" %}

```bash
#!/bin/bash
set -euxo pipefail

until [ -f /opt/instruqt/bootstrap/host-bootstrap-completed ]; do
    echo "Waiting for instruqt bootstrap to complete"    sleep 1
done
```

{% endtab %}
{% endtabs %}

{% hint style="info" %}
The bootstrap process may overwrite user files like `.bashrc`. Wait is encouraged!
{% endhint %}

### Wait for service

Wait for a specific web service in your setup script to finish booting.

{% tabs %}
{% tab title="Bash" %}

```bash
while ! curl --fail --output /dev/null https://<hostname>:<port>/<path> 
do
    sleep 1
done
```

{% endtab %}
{% endtabs %}

### Create a file

Create a file using heredoc method.

{% tabs %}
{% tab title="Bash" %}

```bash
cat > /file/to/create <<EOF
# Some configuration file
setting_one = true
another_setting = false
EOF
```

{% endtab %}
{% endtabs %}

{% hint style="info" %}
You can append to a file by using `>>` as opposed to `>`.&#x20;
{% endhint %}

### Download files

Download files to the local system.

{% tabs %}
{% tab title="Bash (Git)" %}

```bash
git clone https://githost.com/repo <local_dir>
```

{% endtab %}

{% tab title="Bash (Curl)" %}

```bash
curl http://example.com/folder/big-file.iso -O <local_file>
```

{% endtab %}
{% endtabs %}

### Start background process

Start a process in the background so your script does not hang.&#x20;

{% tabs %}
{% tab title="Bash" %}

```bash
nohup ./myprogram > foo.out 2> foo.err < /dev/null & disown
```

{% endtab %}
{% endtabs %}

## Challenge check scripts

### File exists

Check if a file exists:

{% tabs %}
{% tab title="Bash" %}

```bash
#!/bin/bash
set -euxo pipefail

if [ -f /home/user/file.txt ]; then
    echo "The file at /home/user/file.txt exists"
fi
```

{% endtab %}
{% endtabs %}

### File doesn't exist

Check if a file **doesn't** exist (or is not a file):

{% tabs %}
{% tab title="Bash" %}

```bash
#!/bin/bash
set -euxo pipefail

if [ ! -f /home/user/file.txt ]; then
    fail-message "No file was found at /home/user/file.txt"
fi
```

{% endtab %}
{% endtabs %}

### Directory exists

Check if a directory exists:

{% tabs %}
{% tab title="Bash" %}

```bash
#!/bin/bash
set -euxo pipefail

if [ -d /root/folder ]; then
    echo "The directory at /root/folder exists"
fi
```

{% endtab %}
{% endtabs %}

### Directory doesn't exist

Use the following code in your life cycle script to check if a folder **doesn't** exist (or is not a directory)

{% tabs %}
{% tab title="Bash" %}

```bash
#!/bin/bash
set -euxo pipefail

if [ ! -d /root/folder ]; then
    fail-message "The folder at /root/folder doesn't exists"
fi
```

{% endtab %}
{% endtabs %}

### File contains certain text

Check if a file contains certain text.&#x20;

{% tabs %}
{% tab title="Bash" %}

```bash
#!/bin/bash
set -euxo pipefail

if ! grep "text to find" /path/to/file; then
    fail-message "The file doesn't contain the required text"
fi
```

{% endtab %}
{% endtabs %}

### Specific command is installed

Check that a specific command is executable with the -x flag.&#x20;

{% tabs %}
{% tab title="Bash" %}

```bash
#!/bin/bash
set -euxo pipefail

if ! [[ -x /usr/local/bin/command ]]; then
  fail-message "Oops, command was not found or is not executable."
fi
```

{% endtab %}
{% endtabs %}

### Service is running

Check if a service is running.

{% tabs %}
{% tab title="Bash" %}

```bash
#!/bin/bash
if pgrep -x SERVICE >/dev/null; then
    echo "The X service is running"
fi
```

{% endtab %}
{% endtabs %}

{% hint style="info" %}
Replace SERVICE with a service name running on that system.
{% endhint %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.instruqt.com/sandboxes/lifecycle-scripts/lifecycle-scripts-examples.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
