Links

Example scripts

This page describes example scripts that you can use in your challenge lifecycle scripts.
These example scripts 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 the beginning of your track setup script, to block loading the learning environment until bootstrap has been completed.
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

Check if a file exists

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

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):
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

Check if a directory (folder) exists

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

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)
Bash
#!/bin/bash
set -euxo pipefail
if [ ! -d /root/folder ]; then
fail-message "The folder at /root/folder doesn't exists"
fi

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.
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

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.
Bash
#!/bin/bash
set -euxo pipefail
until [ -f /root/file-created.txt ]; do
sleep 1
done
echo "The file at /root/file-created.txt has been created"

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.
Bash
#!/bin/bash
set -euxo pipefail
until [ -d /root/some-folder ]; do
sleep 1
done
echo "The folder at /root/some-folder has been created"

Check if a specific command is installed

Use the following code in your life cycle script to verify that a specific command is executable with the -x flag. Replace /usr/local/bin/command with the path to the command you wish to test for.
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

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.
Bash
#!/bin/bash
while ! nc -z localhost 8080; do
echo "Waiting for port 8080 to become reachable on localhost"
sleep 1
done

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.
Bash
#!/bin/bash
if pgrep -x nginx >/dev/null; then
echo "The nginx service is running"
fi

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!
Bash
#!/bin/bash
cat > /file/to/create <<EOF
# Some configuration file
setting_one = true
another_setting = false
EOF

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.
Bash
#!/bin/bash
cat >> /file/to/append <<EOF
# Some configuration file
setting_three = true
final_setting = false
EOF

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:
nohup ./myprogram > foo.out 2> foo.err < /dev/null & disown
See overcoming hanging for more information.
If you need a simple way to keep processes running between challenges, you can use the GNU Screen program to persist the shell environment.