Example scripts
This page contains a collection of examples that can be used in your challenge life cycle scripts. Please note that these example scripts are written for bash and sometimes use software that might not be available on every operating system.

Wait for Instruqt bootstrap to complete

When starting a sandbox, the Instruqt platform runs a bootstrap script. Add this script to beginning of your track setup script, to block loading the learning environment until bootstrap has been completed.
1
#!/bin/bash
2
set -euxo pipefail
3
4
until [ -f /opt/instruqt/bootstrap/host-bootstrap-completed ]; do
5
echo "Waiting for instruqt bootstrap to complete"
6
sleep 1
7
done
Copied!

Check if a file exists

Use the following check in your lifecycle script to see if a file exists:
1
#!/bin/bash
2
set -euxo pipefail
3
4
if [ -f /home/user/file.txt ]; then
5
echo "The file at /home/user/file.txt exists"
6
fi
Copied!

Check if a file doesn't exist

Use the following code in your life cycle script to check if a file doesn't exist (or is not a file):
1
#!/bin/bash
2
set -euxo pipefail
3
4
if [ ! -f /home/user/file.txt ]; then
5
fail-message "No file was found at /home/user/file.txt"
6
fi
Copied!

Check if a directory (folder) exists

Use the following code in your life cycle script to check if a directory exists:
1
#!/bin/bash
2
set -euxo pipefail
3
4
if [ -d /root/folder ]; then
5
echo "The directory at /root/folder exists"
6
fi
Copied!

Check if a folder 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)
1
#!/bin/bash
2
set -euxo pipefail
3
4
if [ ! -d /root/folder ]; then
5
fail-message "The folder at /root/folder doesn't exists"
6
fi
Copied!

Check if a file contains certain text

Use the following code in your check script to check if a file contains certain text. This example uses the grep command which is handy for searching the contents of files.
1
#!/bin/bash
2
set -euxo pipefail
3
4
if ! grep "text to find" /path/to/file; then
5
fail-message "The file doesn't contain the required text"
6
fi
Copied!

Wait until a file exists

Use the following code in your life cycle script to wait for a file to be created. Use with caution, as this could cause your track to get stuck if the file is never created.
1
#!/bin/bash
2
set -euxo pipefail
3
4
until [ -f /root/file-created.txt ]; do
5
sleep 1
6
done
7
8
echo "The file at /root/file-created.txt has been created"
Copied!

Wait until a folder exists

Use the following code in your life cycle script to wait for a folder to be created. Use with caution, as this could cause your track to get stuck if the folder is never created.
1
#!/bin/bash
2
set -euxo pipefail
3
4
until [ -d /root/some-folder ]; do
5
sleep 1
6
done
7
8
echo "The folder at /root/some-folder has been created"
Copied!

Wait until a port is reachable

Use the following code in your life cycle script to see if a port is reachable. Make sure the environment has the netcat package installed and that you change the script to your desired host and port.
1
#!/bin/bash
2
while ! nc -z localhost 8080; do
3
echo "Waiting for port 8080 to become reachable on localhost"
4
sleep 1
5
done
Copied!

Check if a service is running

Use the following code in your life cycle script to see if a service is running. In this example pgrep was used, but this can be replaced by a different command.
1
#!/bin/bash
2
if pgrep -x nginx >/dev/null; then
3
echo "The nginx service is running"
4
fi
Copied!

Create a file using a HEREDOC

Use the following code to create a file called /file/to/create. All of the text between the EOF markers will be put in the file verbatim. This is known as a Here Document or a HEREDOC. Warning: if the file already exists it will be overwritten!
1
#!/bin/bash
2
3
cat > /file/to/create <<EOF
4
# Some configuration file
5
setting_one = true
6
another_setting = false
7
EOF
Copied!

Append to a file using a HEREDOC

Use the following code to append text to a file called /file/to/append. The only difference between this and the previous example is that we use two >> symbols instead of one. This is generally safer than the previous example because it can still create the file if it doesn't exist and reduces the risk of overwriting an important file.
1
#!/bin/bash
2
3
cat >> /file/to/append <<EOF
4
# Some configuration file
5
setting_three = true
6
final_setting = false
7
EOF
Copied!

Start a process in the background

When starting a process in the background from one of your life cycle scripts, it's important that the process doesn't block or hang. To do so, make sure to redirect all I/O streams (in, out and err), and wrap the command with nohup
Use the following code to start a process in the background:
1
nohup ./myprogram > foo.out 2> foo.err < /dev/null & disown
Copied!
If you need a simple way to keep processes running between challenges, you can use the GNU Screen program to persist the shell environment.
Last modified 29d ago