Problem
You notice your init script takes longer to execute than expected when installing packages using apt or pip.
When you look at the init script logs, it is unclear where the time was spent or which step failed, making it hard to debug.
Cause
Your init script encounters network congestion or transient connectivity issues when it reaches out to external package repositories.
Either congestion or connectivity issues can cause timeouts on commands (such as apt-get install
), or slow or temporarily unavailable external repositories (such as archive.ubuntu.com
or pypi.org
). As a result, you can't easily tell which command failed or how long it actually took.
Solution
Add timestamps and descriptive logs around every major command to:
- See when each step started and finished.
- Measure how long a command/lib installation took.
- Capture exit codes to know if the installation or command has failed.
- Quickly pinpoint where the problem is if the failure happens again.
You can use the following init script with timestamps and descriptive logs added. Replace the following variables.
-
<your-package>
with the package you want to install. -
<your-python-lib>
with any Python library you want to install.
#!/bin/bash
set -euxo pipefail
LOG_PREFIX="[Init Script]"
echo "$(date '+%Y-%m-%d %H:%M:%S') ${LOG_PREFIX} Starting init script"
echo "$(date '+%Y-%m-%d %H:%M:%S') ${LOG_PREFIX} Updating package index..."
apt-get update -y
echo "$(date '+%Y-%m-%d %H:%M:%S') ${LOG_PREFIX} Completed apt-get update with exit code $?"
echo "$(date '+%Y-%m-%d %H:%M:%S') ${LOG_PREFIX} Installing package: <your-package>"
apt-get install -y <your-package>
echo "$(date '+%Y-%m-%d %H:%M:%S') ${LOG_PREFIX} Completed installing <your-package> with exit code $?"
echo "$(date '+%Y-%m-%d %H:%M:%S') ${LOG_PREFIX} Installing Python package: <your-python-lib>"
pip install <your-python-lib>
echo "$(date '+%Y-%m-%d %H:%M:%S') ${LOG_PREFIX} Completed pip install with exit code $?"
echo "$(date '+%Y-%m-%d %H:%M:%S') ${LOG_PREFIX} Init script completed"
After you update your init script, you can view the detailed logs in your workspace. For more information, refer to the Init script logging (AWS | Azure | GCP) documentation.