Knowledge Center Monthly Newsletter - March 2025
Stay up to date with the latest from the Knowledge Center. See all new and updated Knowledge Center articles published in the last month and re:Post’s top contributors.
如果在我尝试安装库时出现生命周期配置超时,如何确保手动安装的库在 Amazon SageMaker 中持久存在?
当我尝试安装其他库时,我的生命周期配置脚本运行的时间超过了 5 分钟。这会导致 Amazon SageMaker 笔记本电脑实例出现超时。我想解决此问题。此外,我想确保我手动安装的库在笔记本电脑实例会话之间持久存在。
简短描述
如果生命周期配置脚本运行时间超过 5 分钟,该脚本将会失败,并且系统将不会创建或启动笔记本电脑实例。
通过下列某一种方法解决此问题:
- nohup:nohup 命令是“no hangup”的缩写,它是一个会忽略挂断信号的 Linux 命令。在此命令的末尾使用 & 符号会强制生命周期配置脚本在后台运行,直到安装软件包为止。此方法建议技术能力较低的用户使用,更适合作为一种短期解决方案。
注意:nohup 命令会忽略挂断信号。因此,必须将它与 & 符号一起使用,脚本才能继续在后台运行。运行生命周期配置脚本的 shell 会在脚本末尾终止。因此,请在命令的开头添加 nohup,并在命令末尾添加 &,以强制生命周期配置脚本在后台运行。 - 在笔记本电脑实例的 Amazon Elastic Block Store(Amazon EBS)卷上创建一个自定义的持久性 Conda 安装:在现有笔记本电脑实例的终端中运行 on-create 脚本。此脚本使用 Miniconda 在 EBS 卷 (/home/ec2-user/SageMaker/) 上创建单独的 Conda 安装。然后运行 on-start 脚本以作为生命周期配置,从而将自定义环境作为 Jupyter 中的内核提供。此方法是技术能力较强的用户的最佳实践,是更好的长期解决方案。
解决方法
使用下面的一种方法来解决生命周期配置超时问题。
运行 nohup 命令
使用 nohup 命令以强制生命周期配置脚本在 5 分钟后超时期限届满后继续在后台运行。确保在命令末尾添加与号(&)。
示例:
#!/bin/bash set -e nohup pip install xgboost &
脚本会在库安装完成后停止运行。发生这种情况时,您不会收到通知,但您可以使用 ps 命令查看脚本是否仍在运行。
**注意:**当您的生命周期配置脚本发生其他超时情形时(例如当您下载大型 Amazon Simple Storage Service (Amazon S3) 对象时),您也可以使用 nohup 命令。
在笔记本实例的 EBS 卷上创建自定义的持久性 Conda 安装
1. 在现有笔记本实例的终端中,使用您的首选编辑器创建一个 .sh 文件。
例如:
vim custom-script.sh
2. 将 on-create 脚本的内容复制到 .sh 文件。此脚本将在一个自定义 Conda 安装中创建一个新的 Conda 环境。此脚本还将在新的 Conda 环境中安装 NumPy 和 Boto3。
注意:该笔记本电脑实例必须连接到互联网以下载 Miniconda 安装程序和 ipykernel。
3. 将此脚本标记为可执行,然后运行脚本。
例如:
chmod +x custom-script.sh ./custom-script.sh
4. 安装完成后,停止笔记本实例。
5. 将启动时脚本复制到 .sh 文件中。
#!/bin/bash set -e # OVERVIEW # This script installs a custom, persistent installation of conda on the Notebook Instance's EBS volume, and ensures # that these custom environments are available as kernels in Jupyter. # # The on-start script uses the custom conda environment created in the on-create script and uses the ipykernel package # to add that as a kernel in Jupyter. # # For another example, see: # https://docs.aws.amazon.com/sagemaker/latest/dg/nbi-add-external.html#nbi-isolated-environment sudo -u ec2-user -i <<'EOF' unset SUDO_UID WORKING_DIR=/home/ec2-user/SageMaker/custom-miniconda/ source "$WORKING_DIR/miniconda/bin/activate" for env in $WORKING_DIR/miniconda/envs/*; do BASENAME=$(basename "$env") source activate "$BASENAME" python -m ipykernel install --user --name "$BASENAME" --display-name "Custom ($BASENAME)" done # Optionally, uncomment these lines to disable SageMaker-provided Conda functionality. # echo "c.EnvironmentKernelSpecManager.use_conda_directly = False" >> /home/ec2-user/.jupyter/jupyter_notebook_config.py # rm /home/ec2-user/.condarc EOF echo "Restarting the Jupyter server.." # For notebook instance with alinux (notebook-al1-v1) initctl restart jupyter-server --no-wait # Use this instead for notebook instance with alinux2 (notebook-al2-v1) systemctl restart jupyter-server
6. 在已停止的笔记本实例上,将 on-start 脚本添加为生命周期配置。每次您启动该笔记本实例时,此脚本会将该自定义环境作为 Jupyter 中的内核提供。
7. 启动笔记本实例,然后在自定义环境中安装自定义库。
例如,要安装 pyarrow:
import sys !conda install --yes --prefix {sys.prefix} -c conda-forge pyarrow
如果您收到提示您需要更新 Conda 的错误消息,则运行以下命令。然后再次尝试安装自定义库。
!conda install -p "/home/ec2-user/anaconda3" "conda>=4.8" --yes !conda install -p "/home/ec2-user/SageMaker/custom-miniconda/miniconda" "conda>=4.8" --yes
如果您停止并重启启动笔记本实例,您的自定义 Conda 环境和库将仍然可用,无需重新安装。
**注意:**您可以使用 Amazon CloudWatch Logs 排查生命周期配置脚本的问题。您可以在 aws/sagemaker/studio 命名空间下的日志流 LifecycleConfigOnStart 中查看脚本执行日志。
相关信息

相关内容
- AWS 官方已更新 3 年前
- AWS 官方已更新 3 年前
- AWS 官方已更新 2 年前
- AWS 官方已更新 3 年前