# Startup scripts

Startup scripts perform tasks during the initial startup process of your instances. They can be used to automatically configure your environment and workloads, alleviating manual setup. See [Instance types & specifications](/compute-and-storage/instance-types-and-specifications.md) for information on what Mithril installs by default on all instances.

{% hint style="warning" %}
Startup scripts only run the *first* time your instance starts up. Your startup script *will not* run again if your instance is relocated or preempted and allocated at a later time. You can use a `systemd` service to [run scripts on boot after relocation or preemption](#running-scripts-on-subsequent-startups). Learn more about relocation and preemption statuses [here](/compute-and-storage/access-and-manage-instances/statuses.md).
{% endhint %}

## Creating a startup script

You have the option to add a startup script each time you create a new reservation or spot bid. Startup scripts must be added before submitting your reservation or bid; they cannot be added later.

You can add a startup script to your reservation or bid in the Mithril console by attaching a bash or txt file or manually typing a script into the startup script field.

Startup scripts must start with `#!/bin/bash` and are limited to a maximum length of 10,000 characters.

## Accessing start-up script and logs

To access your start-up script  from your instance, run the following command:

```bash
cat /var/lib/foundry/startup_script.sh
```

To access any start-up script logs from your instance, run the following command:

```bash
cat /var/log/foundry/startup_script.log
```

## Running scripts on subsequent startups after relocation, preemption, or resuming a spot bid <a href="#running-scripts-on-subsequent-startups" id="running-scripts-on-subsequent-startups"></a>

The startup script added when creating your reservation or spot bid is only executed the first time your instance starts up. **It will not run again if your instance is relocated, preempted, restarted, or resumed.**

However, because Mithril preserves the boot disk for your spot instances until you fully terminate your spot bid, you can use `systemd` services to implement logic that restarts your workload the next time your spot instances start up after being relocated or preempted. You can use your initial startup script to set up a `systemd` service.

Here is a simple example of a startup script that creates a `hello-world.sh` script and a `systemd` service, `startup.service`, which will run `hello-world.sh` on all subsequent startups:

```bash
#!/bin/bash

# Create the .sh file we want to run on startup
sudo tee -a /usr/local/sbin/hello-world.sh >/dev/null << 'EOF'
#!/bin/bash
echo “Hello World!”
EOF

# Make the .sh file executable
sudo chmod +x /usr/local/sbin/hello-world.sh

# Create a .service file to define a systemd service 
sudo tee -a /etc/systemd/system/startup.service >/dev/null << 'EOF'
[Unit]
Description=My Startup Script

[Service]
Type=oneshot
ExecStart=/usr/local/sbin/hello-world.sh

[Install]
WantedBy=multi-user.target
EOF

# Enable services to run on boot
sudo systemctl enable startup.service

echo "hello-world.sh will run on subsequent startups."
```


---

# 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.mithril.ai/compute-and-storage/startup-scripts.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.
