具有多个papermill命令的Bash脚本不会因笔记本错误而失败 [英] Bash script with multiple papermill commands does not fail on notebook errors

查看:64
本文介绍了具有多个papermill命令的Bash脚本不会因笔记本错误而失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个refresh_data.sh文件,其中包含多个造纸厂命令,例如:

I have a refresh_data.sh file which contains multiple papermill commands, for example:

papermill notebook_1.ipynb output_1.ipynb -p start "2017-12-01" -p date "2017-12-31"
papermill notebook_2.ipynb output_2.ipynb -p start "2018-01-01" -p date "2018-01-31"

如果在运行第一个笔记本时出现错误,则该过程将继续执行第二个笔记本.

If I get an error while it is running the first notebook, the process continues executing the second one.

换句话说,一个笔记本中的错误不会破坏"整个脚本.

In other words, an error in one of the notebooks doesn't "break" the overall script.

据我所记得,在普通的python脚本中,如果bash脚本中的命令之一出错,则会中断整个脚本的执行.

As far as I remember with normal python scripts if there is an error in one of the commands within the bash script it breaks the execution of the entire script.

在这种情况下,bash脚本的标准行为是什么?我可以更改它以便它在出现错误时立即停止吗?

What is the standard behaviour of a bash script in this case? Can I change it so that it stops as soon as there is an error?

推荐答案

如果您的bash脚本配置有:set -e,如果命令出错,它将失败:

If your bash script is configured with: set -e it will fail if a command errors out:

在出现错误时自动退出bash shell脚本

#!/bin/bash
set -e
# Any subsequent(*) commands which fail will cause the shell script to exit immediately

您可以使用以下方法来运行纸厂:

You can run papermill using:

--log-output获取有关笔记本故障原因的更多信息.

--log-output to get more information about why your notebook fail.

papermill "${INPUT_NOTEBOOK_PATH}" "${OUTPUT_NOTEBOOK_PATH}" --log-output

要捕获笔记本的执行结果,您始终可以使用$?捕获以前的任何命令的结果:

To capture notebook execution result you can always capture the result of any previous command using $?:

  papermill "${INPUT_NOTEBOOK_PATH}" "${OUTPUT_NOTEBOOK_PATH}" --log-output
  notebook_result=$?
  if [[ ${notebook_result} -eq 0 ]]; then
    echo "All good"
  else
    echo $notebook_result
  fi

这篇关于具有多个papermill命令的Bash脚本不会因笔记本错误而失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆