Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/search/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Linux 将Docker日志尾的最后一行存储到shell变量_Linux_Shell_Docker_Unix_Scripting - Fatal编程技术网

Linux 将Docker日志尾的最后一行存储到shell变量

Linux 将Docker日志尾的最后一行存储到shell变量,linux,shell,docker,unix,scripting,Linux,Shell,Docker,Unix,Scripting,我所要做的就是将docker log的最后一行捕获到shell脚本中的一个变量 以下是我的设置: Dockerfile: 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"] run.sh: FROM python:3.7.2 WORKDIR

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

以下是我的设置:

Dockerfile:

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"]
run.sh:

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"]
#/垃圾箱/垃圾箱
docker构建-t ml/jupyterlab。
docker rm-f ml jupyterlab
docker run-d-p8888:8888-v$(pwd)/src:/workspace——名称ml jupyterlab ml/jupyterlab
直到[[“$url_info”=~“令牌”];做
url_info=$(docker日志ml jupyterlab--尾部1)
睡眠0.1
完成
回显“$url\u信息”

如果有帮助的话,这里是Docker容器中的登录名,我正试图在
$url\u info
中捕获最后一行
http://(6f8bec0aa3d9或127.0.0.1):8888/?token=A6001A419B22D60DD3E525D52794C5BE7C0B368664C3505

[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\u info
保持
null

在0.1s的暂停时间内,可能会生成多个日志文件,包括一个带有“
标记的日志文件”

这意味着最新的一行可能不包括令牌(它已经在日志中,在前面的几行中)

将您的测试替换为

until [[ "${url_info}" != "" ]]; do
    url_info=$(docker logs ml-jupyterlab --tail 100|grep token)
    sleep 0.1 
done

只要
url\u info
为空,就继续测试。

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

#/垃圾箱/垃圾箱
docker构建-t ml/jupyterlab。
docker rm-f ml jupyterlab
docker run-d-p8888:8888-v$(pwd)/src:/workspace——名称ml jupyterlab ml/jupyterlab
直到[[“${url\u info}”!=”];做
docker日志ml jupyterlab&>日志
url|u info=$(cat日志| grep令牌|头-1)
睡眠0.1
完成
回显“$url\u信息”

非常感谢您的帮助。然而,这个解决方案对我没有帮助。如果我运行
url\u info=$(docker记录ml.langiq.goviq.io--tail 100 | grep token)
echo$url\u info
--输出仍然是
换行符