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 the track setup script, to block loading the learning environment until bootstrap has been completed.
1
#!/bin/bash
2
until [ -f /opt/instruqt/bootstrap/host-bootstrap-completed ]; do
3
echo "Waiting for instruqt bootstrap to complete"
4
sleep 1
5
done
Copied!

Check if a file exists

Use the following check in your life cycle script to check if a file exists:
1
#!/bin/bash
2
if [ -f /home/user/file.txt ]; then
3
echo "The file at /home/user/file.txt exists"
4
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 exists:
1
#!/bin/bash
2
if [ ! -f /home/user/file.txt ]; then
3
fail-message "No file was found at /home/user/file.txt"
4
fi
Copied!

Check if a folder exists

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

Check if a folder doesn't exists

Use the following code in your life cycle script to check if a folder doesn't exists:
1
#!/bin/bash
2
if [ ! -d /root/folder ]; then
3
fail-message "The folder at /root/folder doesn't exists"
4
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:
1
#!/bin/bash
2
if ! grep "text to find" /path/to/file; then
3
fail-message "The file doesn't contain the required text"
4
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 might "freeze" your track if the file is never created.
1
#!/bin/bash
2
until [ -f /root/file-created.txt ]; do
3
sleep 1
4
done
5
6
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 might "freeze" your track if the folder is never created.
1
#!/bin/bash
2
until [ -d /root/some-folder ]; do
3
sleep 1
4
done
5
6
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 replace by a different preferred 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

Use the following code to create a file called /file/to/create. All text between the EOF markers will be put in the file verbatim.
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!

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!
Last modified 20d ago