【问题标题】:How to start a shell script after reboot at a specific point of the script如何在脚本的特定点重新启动后启动 shell 脚本
【发布时间】:2018-11-03 22:14:13
【问题描述】:

我是 shell 脚本的新手。 我试图在我的工作中自动安装服务器基本安装,在这种情况下,是一个 freebsd 服务器。

现在,我的问题是,是否可以执行 shell 脚本并在重新启动后在我的脚本被关机中断的最后一点自动继续?

freebsd-update upgrade -r 11.1-RELEASE 
freebsd-update install
shutdown -r now
freebsd-update install

当然,我的脚本中服务器的关闭会停止整个执行,但是在系统再次启动并执行上面命令中的最后一个命令后,是否有机会跳转到脚本中?

freebsd-update install

【问题讨论】:

  • 您可以使用/var/run 跟踪步数。在每个步骤之前,您可以检查步骤文件是否存在并相应地跳过该步骤。如果步骤执行成功,则创建步骤文件。最后,您可以删除所有步骤文件。
  • 谢谢,听起来不错,但我不知道该怎么做。

标签: shell scripting freebsd bsd


【解决方案1】:

你可以这样写:

run_dir=/var/run/myscript # change the name as appropriate    
check_step() {
  step=$1
  step_file=$run_dir/$step.step.done
  if [ ! -f "$step_file" ]; then
    return 0
  else
    echo "Step $step skipped"
    return 1
  fi
}

step_done()  {
  touch "$run_dir/$1.step.done" && echo finished step $1
}

mkdir -p "$run_dir"
check_step upgrade  && freebsd-update upgrade -r 11.1-RELEASE && step_done upgrade
check_step install1 && freebsd-update install && step_done install1
shutdown -r now
check_step install2 && freebsd-update install && step_done install2
  • check_step 检查特定步骤是否已完成
  • step_done 将步骤标记为已完成

【讨论】:

  • Bash 没有安装在 FreeBSD 上的 /bin/bash 中。
  • 更新了答案,使其与 POSIX 兼容。
猜你喜欢
  • 1970-01-01
  • 2022-10-14
  • 2015-06-14
  • 1970-01-01
  • 1970-01-01
  • 2015-08-11
  • 2017-04-07
  • 2016-07-18
  • 2019-03-16
相关资源
最近更新 更多