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.

Check if a file exists

Use the following check in your life cycle script to check if a file exists:

#!/bin/bash
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 exists:

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

Check if a folder exists

Use the following code in your life cycle script to check if a folder exists:

#!/bin/bash
if [ -d /root/folder ]; then
echo "The folder at /root/folder exists"
fi

Check if a folder doesn't exists

Use the following code in your life cycle script to check if a folder doesn't exists:

#!/bin/bash
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:

#!/bin/bash
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 might "freeze" your track if the file is never created.

#!/bin/bash
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 might "freeze" your track if the folder is never created.

#!/bin/bash
until [ -d /root/some-folder ]
do
sleep 1
done
echo "The folder at /root/some-folder has been created"

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.

#!/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 replace by a different preferred command.

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

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.

#!/bin/bash
cat >/file/to/create <<EOF
# Some configuration file
setting_one = true
another_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 also https://en.wikipedia.org/wiki/Nohup#Overcoming_hanging