gpt4 book ai didi

linux - 将最后一行 Docker 日志尾部存储到 shell 变量

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:32:42 26 4
gpt4 key购买 nike

我想做的就是将 docker log 的最后一行捕获到 shell 脚本中的变量。

这是我的设置:

Docker 文件:

FROM python:3.7.2
WORKDIR /workspace
RUN pip install jupyterlab
EXPOSE 8888
ENTRYPOINT ["jupyter", "lab", "--ip=0.0.0.0", "--allow-root", "--no-browser"]

运行.sh:

#!/bin/sh

docker build -t ml/jupyterlab .
docker rm -f ml-jupyterlab
docker run -d -p 8888:8888 -v $(pwd)/src:/workspace --name ml-jupyterlab ml/jupyterlab

until [[ "$url_info" =~ "token" ]]; do
url_info=$(docker logs ml-jupyterlab --tail 1)
sleep 0.1
done

echo "$url_info"

如果有帮助,这里是来自 Docker 容器的登录,我正在 try catch 最后一行 http://(6f8bec0aa3d9 or 127.0.0.1):8888/?token=a6001a419b22d60dd3e5215d52794c5be7c0b368664c3505$url_info

[I 05:22:37.925 LabApp] Writing notebook server cookie secret to /root/.local/share/jupyter/runtime/notebook_cookie_secret
[I 05:22:39.190 LabApp] JupyterLab extension loaded from /usr/local/lib/python3.7/site-packages/jupyterlab
[I 05:22:39.190 LabApp] JupyterLab application directory is /usr/local/share/jupyter/lab
[W 05:22:39.194 LabApp] JupyterLab server extension not enabled, manually loading...
[I 05:22:39.198 LabApp] JupyterLab extension loaded from /usr/local/lib/python3.7/site-packages/jupyterlab
[I 05:22:39.198 LabApp] JupyterLab application directory is /usr/local/share/jupyter/lab
[I 05:22:39.199 LabApp] Serving notebooks from local directory: /workspace
[I 05:22:39.199 LabApp] The Jupyter Notebook is running at:
[I 05:22:39.200 LabApp] http://(6f8bec0aa3d9 or 127.0.0.1):8888/?token=a6001a419b22d60dd3e5215d52794c5be7c0b368664c3505
[I 05:22:39.200 LabApp] Use Control-C to stop this server and shut down all kernels (twice to skip confirmation).
[C 05:22:39.204 LabApp]

To access the notebook, open this file in a browser:
file:///root/.local/share/jupyter/runtime/nbserver-1-open.html
Or copy and paste one of these URLs:
http://(6f8bec0aa3d9 or 127.0.0.1):8888/?token=a6001a419b22d60dd3e5215d52794c5be7c0b368664c3505

执行以下操作

$ chmod +x run.sh
$ ./run.sh

进入无限循环,因为 $url_info 仍然是 null

最佳答案

最后,我使用以下脚本解决了这个问题:

#!/bin/sh

docker build -t ml/jupyterlab .
docker rm -f ml-jupyterlab
docker run -d -p 8888:8888 -v $(pwd)/src:/workspace --name ml-jupyterlab ml/jupyterlab

until [[ "${url_info}" != "" ]]; do
docker logs ml-jupyterlab &> logs
url_info=$(cat logs | grep token | head -1)
sleep 0.1
done


echo "$url_info"

关于linux - 将最后一行 Docker 日志尾部存储到 shell 变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55331666/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com